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