1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18 import static org.htmlunit.junit.annotation.TestedBrowser.FF;
19 import static org.htmlunit.junit.annotation.TestedBrowser.FF_ESR;
20
21 import java.net.URL;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.htmlunit.HttpHeader;
26 import org.htmlunit.WebDriverTestCase;
27 import org.htmlunit.junit.BrowserRunner;
28 import org.htmlunit.junit.annotation.Alerts;
29 import org.htmlunit.junit.annotation.NotYetImplemented;
30 import org.htmlunit.util.MimeType;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.openqa.selenium.By;
34 import org.openqa.selenium.NoSuchElementException;
35 import org.openqa.selenium.WebDriver;
36 import org.openqa.selenium.WebElement;
37
38
39
40
41
42
43
44
45
46
47 @RunWith(BrowserRunner.class)
48 public class HtmlPage3Test extends WebDriverTestCase {
49
50
51
52
53 @Test
54 public void emptyJavaScript() throws Exception {
55 final String html = "<body>\n"
56 + "<a id='myAnchor' href='javascript:'>Hello</a>\n"
57 + "</body>";
58
59 final WebDriver driver = loadPage2(html);
60 driver.findElement(By.id("myAnchor")).click();
61 }
62
63
64
65
66
67 @Test
68 public void formElementCreatedFromJavascript() throws Exception {
69 final String html = DOCTYPE_HTML
70 + "<html>\n"
71 + "<head>\n"
72 + "<script type='text/javascript'>\n"
73 + " function modifyForm() {\n"
74 + " var myForm = document.forms['test_form'];\n"
75 + " var el = document.createElement('input');\n"
76 + " el.setAttribute('addedBy','js');\n"
77 + " el.name = 'myHiddenField';\n"
78 + " el.value = 'myValue';\n"
79 + " el.type = 'hidden';\n"
80 + " myForm.appendChild(el);\n"
81 + "}\n"
82 + "</script>\n"
83 + "</head>\n"
84 + "<body onLoad='modifyForm()'>\n"
85 + " <form id='test_form' action='http://www.sourceforge.com/' method='post'>\n"
86 + " <input type='submit' value='click'/>\n"
87 + " </form>\n"
88 + "</body>\n"
89 + "</html>";
90
91 final WebDriver driver = loadPage2(html);
92 final List<WebElement> elements = driver.findElements(By.xpath("//*"));
93 assertEquals(7, elements.size());
94
95 assertEquals("html", elements.get(0).getTagName());
96 assertEquals("head", elements.get(1).getTagName());
97 assertEquals("script", elements.get(2).getTagName());
98 assertEquals("body", elements.get(3).getTagName());
99 assertEquals("form", elements.get(4).getTagName());
100 assertEquals("input", elements.get(5).getTagName());
101
102 final WebElement input = elements.get(6);
103 assertEquals("input", input.getTagName());
104 assertEquals("myHiddenField", input.getAttribute("name"));
105 assertEquals("js", input.getAttribute("addedBy"));
106 assertEquals("js", input.getAttribute("addedby"));
107 }
108
109
110
111
112 @Test
113 @Alerts({"windows-1252", "windows-1252", "windows-1252", "undefined"})
114 public void getPageEncoding() throws Exception {
115 final String htmlContent = DOCTYPE_HTML
116 + "<html><head>\n"
117 + " <meta http-equiv='Content-Type' content='text/html; charset=Shift_JIS'>\n"
118 + " <script>\n"
119 + LOG_TITLE_FUNCTION
120 + " function test() {\n"
121 + " log(document.inputEncoding);\n"
122 + " log(document.characterSet);\n"
123 + " log(document.charset);\n"
124 + " log(document.defaultCharset);\n"
125 + " }\n"
126 + " </script>\n"
127 + "</head><body onload='test()'>\n"
128 + "<table><tr><td>\n"
129 + "<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">\n"
130 + "<form name='form1'>\n"
131 + " <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
132 + " <input type='text' name='textfield2' id='textfield2'/>\n"
133 + "</form>\n"
134 + "</td></tr></table>\n"
135 + "</body></html>";
136 loadPageVerifyTitle2(htmlContent);
137 }
138
139
140
141
142
143 @Test
144 public void onLoadHandler_ScriptNameRead() throws Exception {
145 final String html = DOCTYPE_HTML
146 + "<html><head><title>foo</title>\n"
147 + "<script type='text/javascript'>\n"
148 + " load = function() {};\n"
149 + " onload = load;\n"
150 + " alert(onload);\n"
151 + "</script></head><body></body></html>";
152
153 final WebDriver driver = loadPage2(html);
154 final List<String> alerts = getCollectedAlerts(driver, 1);
155 assertEquals(1, alerts.size());
156 assertTrue(alerts.get(0).startsWith("function"));
157 }
158
159
160
161
162 @Test
163 public void constructor() throws Exception {
164 final String html = DOCTYPE_HTML
165 + "<html>\n"
166 + "<head><title>foo</title></head>\n"
167 + "<body>\n"
168 + "<p>hello world</p>\n"
169 + "<form id='form1' action='/formSubmit' method='post'>\n"
170 + " <input type='text' NAME='textInput1' value='textInput1'/>\n"
171 + " <input type='text' name='textInput2' value='textInput2'/>\n"
172 + " <input type='hidden' name='hidden1' value='hidden1'/>\n"
173 + " <input type='submit' name='submitInput1' value='push me'/>\n"
174 + "</form>\n"
175 + "</body></html>";
176
177 final WebDriver driver = loadPage2(html);
178 assertTitle(driver, "foo");
179 }
180
181
182
183
184 @Test
185 public void getInputByName() throws Exception {
186 final String html = DOCTYPE_HTML
187 + "<html>\n"
188 + "<head><title>foo</title></head>\n"
189 + "<body>\n"
190 + "<p>hello world</p>\n"
191 + "<form id='form1' action='/formSubmit' method='post'>\n"
192 + " <input type='text' NAME='textInput1' value='textInput1'/>\n"
193 + " <input type='text' name='textInput2' value='textInput2'/>\n"
194 + " <input type='hidden' name='hidden1' value='hidden1'/>\n"
195 + " <input type='submit' name='submitInput1' value='push me'/>\n"
196 + "</form>\n"
197 + "</body></html>";
198
199 final WebDriver driver = loadPage2(html);
200
201 final WebElement form = driver.findElement(By.id("form1"));
202 final WebElement input = form.findElement(By.name("textInput1"));
203 assertEquals("name", "textInput1", input.getDomAttribute("name"));
204
205 assertEquals("value", "textInput1", input.getDomAttribute("value"));
206 assertEquals("type", "text", input.getDomAttribute("type"));
207 }
208
209
210
211
212 @Test
213 @Alerts({"[object HTMLInputElement]", "1"})
214 public void write_getElementById_afterParsing() throws Exception {
215 final String html = DOCTYPE_HTML
216 + "<html>\n"
217 + "<head>\n"
218 + "<script>\n"
219 + LOG_WINDOW_NAME_FUNCTION
220 + " function test() {\n"
221 + " document.write(\"<input id='sendemail'>\");\n"
222 + " log(document.getElementById('sendemail'));\n"
223 + " document.write(\"<input name='sendemail2'>\");\n"
224 + " log(document.getElementsByName('sendemail2').length);\n"
225 + " }\n"
226 + "</script></head>\n"
227 + "<body onload='test()'>\n"
228 + "</body></html>";
229
230 loadPage2(html);
231 verifyWindowName2(getWebDriver(), getExpectedAlerts());
232 }
233
234
235
236
237 @Test
238 @Alerts({"[object HTMLInputElement]", "1"})
239 public void write_getElementById_duringParsing() throws Exception {
240 final String html = DOCTYPE_HTML
241 + "<html>\n"
242 + "<head></head>\n"
243 + "<body><script>\n"
244 + LOG_TITLE_FUNCTION
245 + " document.write(\"<input id='sendemail'>\");\n"
246 + " log(document.getElementById('sendemail'));\n"
247 + " document.write(\"<input name='sendemail2'>\");\n"
248 + " log(document.getElementsByName('sendemail2').length);\n"
249 + "</script></body></html>";
250 loadPageVerifyTitle2(html);
251 }
252
253
254
255
256 @Test
257 @Alerts("Hello")
258 public void application_javascript_type() throws Exception {
259 final String html = DOCTYPE_HTML
260 + "<html>\n"
261 + "<body>\n"
262 + " <script type='application/javascript'>\n"
263 + LOG_TITLE_FUNCTION
264 + " log('Hello');\n"
265 + " </script>\n"
266 + "</body></html>";
267
268 loadPageVerifyTitle2(html);
269 }
270
271
272
273
274 @Test
275 @Alerts("Hello")
276 public void application_x_javascript_type() throws Exception {
277 final String html = DOCTYPE_HTML
278 + "<html>\n"
279 + "<body>\n"
280 + " <script type='application/x-javascript'>\n"
281 + LOG_TITLE_FUNCTION
282 + " log('Hello');\n"
283 + " </script>\n"
284 + "</body></html>";
285
286 loadPageVerifyTitle2(html);
287 }
288
289
290
291
292 @Test
293 public void basePath() throws Exception {
294 basePath("base_path", URL_SECOND + "path");
295 }
296
297 private void basePath(final String baseUrl, final String expected) throws Exception {
298 final String html = DOCTYPE_HTML
299 + "<html>\n"
300 + "<head>\n"
301 + " <base href='" + baseUrl + "'>\n"
302 + "</head>\n"
303 + "<body>\n"
304 + " <a id='testLink' href='path'>click me</a>\n"
305 + "</body></html>";
306 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
307 final WebDriver webDriver = loadPage2(html, URL_SECOND);
308 webDriver.findElement(By.id("testLink")).click();
309 assertEquals(expected, webDriver.getCurrentUrl());
310 }
311
312
313
314
315 @Test
316 public void basePathAndSlash() throws Exception {
317 basePath("base_path/", URL_SECOND + "base_path/path");
318 }
319
320
321
322
323 @Test
324 public void basePathAfterSlash() throws Exception {
325 basePath("/base_path", "http://localhost:" + PORT + "/path");
326 }
327
328
329
330
331 @Test
332 public void basePathSlashes() throws Exception {
333 basePath("/base_path/", URL_FIRST + "base_path/path");
334 }
335
336
337
338
339 @Test
340 public void basePathFullyQualified() throws Exception {
341 basePath("http://localhost:" + PORT + "/base_path", "http://localhost:" + PORT + "/path");
342 }
343
344
345
346
347 @Test
348 public void basePathFullyQualifiedSlash() throws Exception {
349 basePath("http://localhost:" + PORT + "/base_path/", "http://localhost:" + PORT + "/base_path/path");
350 }
351
352
353
354
355 @Test
356
357 public void basePathNoProtocol() throws Exception {
358 basePath("//localhost:" + PORT + "/base_path", "http://localhost:" + PORT + "/path");
359 }
360
361
362
363
364 @Test
365 public void basePathNoProtocolSlash() throws Exception {
366 basePath("//localhost:" + PORT + "/base_path/", "http://localhost:" + PORT + "/base_path/path");
367 }
368
369
370
371
372 @Test
373 public void basePathInvalid() throws Exception {
374 basePath("---****://==", URL_SECOND + "---****://path");
375 }
376
377
378
379
380 @Test
381 public void basePathLeadingAndTrailingWhitespace() throws Exception {
382 basePath(" \t\n" + "http://localhost:" + PORT + "/base_path/" + "\n\t ",
383 "http://localhost:" + PORT + "/base_path/path");
384 }
385
386
387
388
389 @Test
390 public void basePathEmpty() throws Exception {
391 basePath("", "http://localhost:" + PORT + "/second/path");
392 }
393
394
395
396
397 @Test
398 public void basePathWhitespaceOnly() throws Exception {
399 basePath(" \t\n ", "http://localhost:" + PORT + "/second/path");
400 }
401
402
403
404
405 @Test
406 @Alerts({"[object SVGSVGElement]", "http://www.w3.org/2000/svg",
407 "[object SVGRectElement]", "http://www.w3.org/2000/svg"})
408 public void htmlPageEmbeddedSvgWithoutNamespace() throws Exception {
409 final String content
410 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
411 + "<head>\n"
412 + "<script>\n"
413 + LOG_TITLE_FUNCTION
414 + " function test() {\n"
415 + " log(document.getElementById('mySvg'));\n"
416 + " log(document.getElementById('mySvg').namespaceURI);\n"
417 + " log(document.getElementById('myRect'));\n"
418 + " log(document.getElementById('myRect').namespaceURI);\n"
419 + " }\n"
420 + "</script>\n"
421 + "</head>\n"
422 + "<body onload='test()'>\n"
423 + " <svg id='mySvg'>\n"
424 + " <rect id='myRect' />\n"
425 + " </svg>\n"
426 + "</body>\n"
427 + "</html>";
428
429 loadPageVerifyTitle2(content);
430 }
431
432
433
434
435 @Test
436 @Alerts("HTML")
437 public void htmlPage() throws Exception {
438 final String content
439 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
440 + "<svg xmlns=\"http://www.w3.org/2000/svg\">\n"
441 + " <rect id='rect' width='50' height='50' fill='green' />\n"
442 + "<head>\n"
443 + "<script>\n"
444 + LOG_TITLE_FUNCTION
445 + " function test() {\n"
446 + " log(document.documentElement.tagName);\n"
447 + " }\n"
448 + "</script>\n"
449 + "</head>\n"
450 + "<body onload='test()'>\n"
451 + "</body>\n"
452 + "</svg>";
453
454 loadPageVerifyTitle2(content);
455 }
456
457
458
459
460 @Test
461 @Alerts("[object HTMLHtmlElement]")
462 public void htmlSvgPage() throws Exception {
463 final String content
464 = "<html xmlns=\"http://www.w3.org/2000/svg\">\n"
465 + " <rect id='rect' width='50' height='50' fill='green' />\n"
466 + "<body>\n"
467 + "<script>\n"
468 + LOG_TITLE_FUNCTION
469 + " log(document.documentElement);\n"
470 + "</script>\n"
471 + "</body>\n"
472 + "</html>";
473
474 loadPageVerifyTitle2(content);
475 }
476
477
478
479
480 @Test
481 @Alerts(DEFAULT = "error",
482 CHROME = "Something",
483 EDGE = "Something")
484 @NotYetImplemented({FF, FF_ESR})
485 public void shouldBeAbleToFindElementByXPathInXmlDocument() throws Exception {
486 final String html = "<?xml version='1.0' encoding='UTF-8'?>\n"
487 + "<html xmlns='http://www.w3.org/1999/xhtml'\n"
488 + " xmlns:svg='http://www.w3.org/2000/svg'\n"
489 + " xmlns:xlink='http://www.w3.org/1999/xlink'>\n"
490 + "<body>\n"
491 + " <svg:svg id='chart_container' height='220' width='400'>\n"
492 + " <svg:text y='16' x='200' text-anchor='middle'>Something</svg:text>\n"
493 + " </svg:svg>\n"
494 + "</body>\n"
495 + "</html>\n";
496
497 final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1, null);
498 String actual;
499 try {
500 final WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));
501 actual = element.getText();
502 }
503 catch (final NoSuchElementException e) {
504 actual = "error";
505 }
506 assertEquals(getExpectedAlerts()[0], actual);
507 }
508
509
510
511
512 @Test
513 @Alerts("interactive")
514 public void readyStateInDOMContentLoaded() throws Exception {
515 final String html = DOCTYPE_HTML
516 + "<html>\n"
517 + " <head>\n"
518 + " <script>\n"
519 + LOG_TITLE_FUNCTION
520 + " document.addEventListener('DOMContentLoaded', function () {\n"
521 + " log(document.readyState);\n"
522 + " });\n"
523 + " </script>\n"
524 + " </head>\n"
525 + " <body>test</body>\n"
526 + "</html>";
527
528 loadPageVerifyTitle2(html);
529 }
530
531
532
533
534 @Test
535 @Alerts("25")
536 public void loadExternalJavaScript() throws Exception {
537 final String html = DOCTYPE_HTML
538 + "<html><head>\n"
539 + "<script>\n"
540 + "function makeIframe() {\n"
541 + " var iframesrc = '<html><head>';\n"
542 + " iframesrc += '<script src=\"" + "js.js" + "\"></' + 'script>';\n"
543 + " iframesrc += '<script>';\n"
544 + " iframesrc += 'function doSquared() {';\n"
545 + " iframesrc += ' try {';\n"
546 + " iframesrc += ' var y = squared(5);';\n"
547 + " iframesrc += ' alert(y);';\n"
548 + " iframesrc += ' } catch(e) {';\n"
549 + " iframesrc += ' alert(\"error\");';\n"
550 + " iframesrc += ' }';\n"
551 + " iframesrc += '}';\n"
552 + " iframesrc += '</' + 'script>';\n"
553 + " iframesrc += '</head>';\n"
554 + " iframesrc += '<body onLoad=\"doSquared()\" >';\n"
555 + " iframesrc += '</body>';\n"
556 + " iframesrc += '</html>';\n"
557 + " var iframe = document.createElement('IFRAME');\n"
558 + " iframe.id = 'iMessage';\n"
559 + " iframe.name = 'iMessage';\n"
560 + " iframe.src = \"javascript:'\" + iframesrc + \"'\";\n"
561 + " document.body.appendChild(iframe);\n"
562 + "}\n"
563 + "</script></head>\n"
564 + "<body onload='makeIframe()'>\n"
565 + "</body></html>";
566
567 final String js = "function squared(n) {return n * n}";
568
569 getMockWebConnection().setResponse(URL_FIRST, html);
570 getMockWebConnection().setResponse(new URL(URL_FIRST, "js.js"), js);
571
572 loadPageWithAlerts2(URL_FIRST);
573
574 assertEquals(2, getMockWebConnection().getRequestCount());
575
576 final Map<String, String> lastAdditionalHeaders = getMockWebConnection().getLastAdditionalHeaders();
577 assertNull(lastAdditionalHeaders.get(HttpHeader.REFERER));
578 }
579
580
581
582
583
584 @Test
585 @Alerts("25")
586 public void loadExternalJavaScript_absolute() throws Exception {
587 final String html = DOCTYPE_HTML
588 + "<html><head>\n"
589 + "<script>\n"
590 + "function makeIframe() {\n"
591 + " var iframesrc = '<html><head>';\n"
592 + " iframesrc += '<script src=\"" + URL_SECOND + "\"></' + 'script>';\n"
593 + " iframesrc += '<script>';\n"
594 + " iframesrc += 'function doSquared() {';\n"
595 + " iframesrc += ' try {';\n"
596 + " iframesrc += ' var y = squared(5);';\n"
597 + " iframesrc += ' alert(y);';\n"
598 + " iframesrc += ' } catch(e) {';\n"
599 + " iframesrc += ' log(\"error\");';\n"
600 + " iframesrc += ' }';\n"
601 + " iframesrc += '}';\n"
602 + " iframesrc += '</' + 'script>';\n"
603 + " iframesrc += '</head>';\n"
604 + " iframesrc += '<body onLoad=\"doSquared()\" >';\n"
605 + " iframesrc += '</body>';\n"
606 + " iframesrc += '</html>';\n"
607 + " var iframe = document.createElement('IFRAME');\n"
608 + " iframe.id = 'iMessage';\n"
609 + " iframe.name = 'iMessage';\n"
610 + " iframe.src = \"javascript:'\" + iframesrc + \"'\";\n"
611 + " document.body.appendChild(iframe);\n"
612 + "}\n"
613 + "</script></head>\n"
614 + "<body onload='makeIframe()'>\n"
615 + "</body></html>";
616
617 final String js = "function squared(n) {return n * n}";
618
619 getMockWebConnection().setResponse(URL_FIRST, html);
620 getMockWebConnection().setResponse(URL_SECOND, js);
621
622 loadPageWithAlerts2(URL_FIRST);
623
624 assertEquals(2, getMockWebConnection().getRequestCount());
625
626 final Map<String, String> lastAdditionalHeaders = getMockWebConnection().getLastAdditionalHeaders();
627 assertNull(lastAdditionalHeaders.get(HttpHeader.REFERER));
628 }
629
630
631
632
633
634 @Test
635 @Alerts({"cl2", "cl1"})
636 public void onLoadHandler_idChange() throws Exception {
637 final String html = DOCTYPE_HTML
638 + "<html>\n"
639 + "<head>\n"
640 + "<div id='id1' class='cl1'><div id='id2' class='cl2'></div></div>'"
641 + "<script type='text/javascript'>\n"
642 + LOG_TITLE_FUNCTION
643 + "document.getElementById('id1').id = 'id3';\n"
644 + "log(document.getElementById('id2').className);\n"
645 + "log(document.getElementById('id3').className);\n"
646 + "</script>\n"
647 + "</head><body></body></html>";
648
649 loadPageVerifyTitle2(html);
650 }
651
652
653
654
655
656
657
658 @Test
659 @Alerts("[object HTMLTableRowElement]")
660 public void getElementById_AfterAppendRemoveAppendChild() throws Exception {
661 final String content = DOCTYPE_HTML
662 + "<html><head>\n"
663 + "<script>\n"
664 + LOG_TITLE_FUNCTION
665 + " function test() {\n"
666 + " var table = document.createElement('table');\n"
667 + " var tr = document.createElement('tr');\n"
668 + " tr.id = 'myTR';\n"
669 + " table.appendChild(tr);\n"
670 + " document.body.appendChild(table);\n"
671 + " document.body.removeChild(table);\n"
672 + " document.body.appendChild(table);\n"
673 + " log(document.getElementById('myTR'));\n"
674 + " }\n"
675 + "</script></head>\n"
676 + "<body onload='test()'>\n"
677 + "</body></html>";
678 loadPageVerifyTitle2(content);
679 }
680
681
682
683
684 @Test
685 @Alerts("null")
686 public void getElementById_AfterAppendingToNewlyCreatedElement() throws Exception {
687 final String content = DOCTYPE_HTML
688 + "<html><head>\n"
689 + "<script>\n"
690 + LOG_TITLE_FUNCTION
691 + " function test() {\n"
692 + " var table = document.createElement('table');\n"
693 + " var tr = document.createElement('tr');\n"
694 + " tr.id = 'myTR';\n"
695 + " table.appendChild(tr);\n"
696 + " log(document.getElementById('myTR'));\n"
697 + " }\n"
698 + "</script></head>\n"
699 + "<body onload='test()'>\n"
700 + "</body></html>";
701 loadPageVerifyTitle2(content);
702 }
703
704
705
706
707
708 @Test
709 @Alerts("works")
710 public void metaWithNamespace() throws Exception {
711 final String content = DOCTYPE_HTML
712 + "<html>\n"
713 + "<head>\n"
714 + " <title>works\u00a7</title>\n"
715 + "</head>\n"
716 + "<body>\n"
717 + " <overheidrg:meta xmlns:overheidrg='http://standaarden.overheid.nl/cvdr/terms/'>\n"
718 + "</body>\n"
719 + "</html>";
720
721 loadPageVerifyTitle2(content);
722 }
723 }