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 java.nio.charset.StandardCharsets.UTF_8;
19
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.Map;
23
24 import org.apache.commons.io.ByteOrderMark;
25 import org.htmlunit.HttpHeader;
26 import org.htmlunit.WebDriverTestCase;
27 import org.htmlunit.http.HttpStatus;
28 import org.htmlunit.junit.BrowserRunner;
29 import org.htmlunit.junit.annotation.Alerts;
30 import org.htmlunit.junit.annotation.HtmlUnitNYI;
31 import org.htmlunit.util.MimeType;
32 import org.htmlunit.util.NameValuePair;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.openqa.selenium.By;
36 import org.openqa.selenium.WebDriver;
37
38
39
40
41
42
43
44
45
46
47
48 @RunWith(BrowserRunner.class)
49 public class HtmlScript2Test extends WebDriverTestCase {
50
51
52
53
54 @Test
55 @Alerts("myValue")
56 public void insertBefore() throws Exception {
57 final String html = DOCTYPE_HTML
58 + "<html><head>\n"
59 + "<script>\n"
60 + LOG_TITLE_FUNCTION
61 + " function test() {\n"
62 + " var script = document.createElement('script');\n"
63 + " script.text = \"foo = 'myValue';\";\n"
64 + " document.body.insertBefore(script, document.body.firstChild);\n"
65 + " log(foo);\n"
66 + " }\n"
67 + "</script>\n"
68 + "</head>\n"
69 + "<body onload='test()'></body>\n"
70 + "</html>";
71
72 loadPageVerifyTitle2(html);
73 }
74
75
76
77
78 @Test
79 @Alerts({"created", "hello", "replaced"})
80 public void addedFromDocumentFragment() throws Exception {
81 final String html = DOCTYPE_HTML
82 + "<html><body>\n"
83 + "<span id='A'></span>\n"
84 + "<script>\n"
85 + LOG_TITLE_FUNCTION
86 + "var text = '<script>log(\"hello\");</sc' + 'ript>';\n"
87 + "var element = document.getElementById('A');\n"
88 + "try {\n"
89 + " var range = element.ownerDocument.createRange();\n"
90 + " range.selectNode(element);\n"
91 + " var fragment = range.createContextualFragment(text);\n"
92 + " log('created');\n"
93 + " element.parentNode.replaceChild(fragment, element);\n"
94 + " log('replaced');\n"
95 + "} catch(e) { logEx(e); }\n"
96 + "</script></body></html>";
97
98 loadPageVerifyTitle2(html);
99 }
100
101
102
103
104 @Test
105 @Alerts("[object HTMLScriptElement]")
106 public void simpleScriptable() throws Exception {
107 final String html = DOCTYPE_HTML
108 + "<html><head>\n"
109 + "<script>\n"
110 + LOG_TITLE_FUNCTION
111 + " function test() {\n"
112 + " log(document.getElementById('myId'));\n"
113 + " }\n"
114 + "</script>\n"
115 + "</head><body onload='test()'>\n"
116 + " <script id='myId'></script>\n"
117 + "</body></html>";
118
119 final WebDriver driver = loadPageVerifyTitle2(html);
120 assertEquals("script", driver.findElement(By.id("myId")).getTagName());
121 }
122
123
124
125
126 @Test
127 @Alerts("Hello")
128 public void type_case_sensitivity() throws Exception {
129 final String html = DOCTYPE_HTML
130 + "<html>\n"
131 + "<body>\n"
132 + " <script type='text/JavaScript'>\n"
133 + LOG_TITLE_FUNCTION
134 + " log('Hello');\n"
135 + " </script>\n"
136 + "</body></html>";
137
138 loadPageVerifyTitle2(html);
139 }
140
141
142
143
144
145 @Test
146 @Alerts({"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G"})
147 public void typeValues() throws Exception {
148 final String html = DOCTYPE_HTML
149 + "<html>"
150 + "<head>\n"
151 + "<script>\n"
152 + LOG_TITLE_FUNCTION
153 + "</script>\n"
154 + "</head>\n"
155 + "<body>\n"
156 + " <script type='application/javascript'>log('1');</script>\n"
157 + " <script type='application/ecmascript'>log('2');</script>\n"
158 + " <script type='application/x-ecmascript'>log('3');</script>\n"
159 + " <script type='application/x-javascript'>log('4');</script>\n"
160 + " <script type='text/javascript'>log('5');</script>\n"
161 + " <script type='text/ecmascript'>log('6');</script>\n"
162 + " <script type='text/javascript1.0'>log('7');</script>\n"
163 + " <script type='text/javascript1.1'>log('8');</script>\n"
164 + " <script type='text/javascript1.2'>log('9');</script>\n"
165 + " <script type='text/javascript1.3'>log('A');</script>\n"
166 + " <script type='text/javascript1.4'>log('B');</script>\n"
167 + " <script type='text/javascript1.5'>log('C');</script>\n"
168 + " <script type='text/jscript'>log('D');</script>\n"
169 + " <script type='text/livescript'>log('E');</script>\n"
170 + " <script type='text/x-ecmascript'>log('F');</script>\n"
171 + " <script type='text/x-javascript'>log('G');</script>\n"
172 + "</body></html>";
173
174 loadPageVerifyTitle2(html);
175 }
176
177
178
179
180 @Test
181 @Alerts({"1", "5", "7"})
182 public void type_language() throws Exception {
183 final String html = DOCTYPE_HTML
184 + "<html>\n"
185 + "<head>\n"
186 + "<script>\n"
187 + LOG_TITLE_FUNCTION
188 + "</script>\n"
189 + "</head>\n"
190 + "<body>\n"
191 + " <script>\n"
192 + " log('1');\n"
193 + " </script>\n"
194 + " <script language='anything'>\n"
195 + " log('2');\n"
196 + " </script>\n"
197 + " <script type='anything'>\n"
198 + " log('3');\n"
199 + " </script>\n"
200 + " <script language='anything' type='anything'>\n"
201 + " log('4');\n"
202 + " </script>\n"
203 + " <script language='anything' type='text/javascript'>\n"
204 + " log('5');\n"
205 + " </script>\n"
206 + " <script language='javascript' type='anything'>\n"
207 + " log('6');\n"
208 + " </script>\n"
209 + " <script language='javascript'>\n"
210 + " log('7');\n"
211 + " </script>\n"
212 + "</body></html>";
213
214 loadPageVerifyTitle2(html);
215 }
216
217
218
219
220
221
222 @Test
223 @Alerts("a")
224 public void scriptIsNotRunWhenCloned() throws Exception {
225 final String html = DOCTYPE_HTML
226 + "<html>\n"
227 + "<head>\n"
228 + "<script>\n"
229 + LOG_TITLE_FUNCTION
230 + "</script>\n"
231 + "</head>\n"
232 + "<body onload='document.body.cloneNode(true)'>\n"
233 + "<script>log('a')</script></body></html>";
234
235 loadPageVerifyTitle2(html);
236 }
237
238
239
240
241 @Test
242 @Alerts({"deferred", "start", "dcl listener added", "end", "dcLoaded", "onload"})
243 public void deferInline() throws Exception {
244 final String html = DOCTYPE_HTML
245 + "<html>\n"
246 + "<head>\n"
247 + " <script>\n"
248 + LOG_TITLE_FUNCTION
249 + " </script>\n"
250 + " <script defer>log('deferred')</script>\n"
251 + " <script>log('start')</script>"
252 + " <script>\n"
253 + " document.addEventListener('DOMContentLoaded', function(event) { log('dcLoaded') });\n"
254 + " log('dcl listener added')</script>"
255 + " </script>\n"
256 + "</head>\n"
257 + "<body onload='log(\"onload\")'>\n"
258 + "</body>\n"
259 + "<script>log('end')</script>\n"
260 + "</html>";
261
262 loadPageVerifyTitle2(html);
263 }
264
265
266
267
268 @Test
269 @Alerts({"start", "dcl listener added", "end", "deferred-1", "deferred-2", "deferred-3", "dcLoaded", "onload"})
270 public void deferExternal() throws Exception {
271 final String html = DOCTYPE_HTML
272 + "<html>\n"
273 + "<head>\n"
274 + " <script>\n"
275 + LOG_TITLE_FUNCTION
276 + " </script>\n"
277 + " <script defer src='script1.js'></script>\n"
278 + " <script>log('start')</script>"
279 + " <script>\n"
280 + " document.addEventListener('DOMContentLoaded', function(event) { log('dcLoaded') });\n"
281 + " log('dcl listener added')</script>"
282 + " </script>\n"
283 + " <script defer src='script2.js'></script>\n"
284 + "</head>\n"
285 + "<body onload='log(\"onload\")'>\n"
286 + " <div id='abc'>Test</div>\n"
287 + "</body>\n"
288 + "<script defer src='script3.js'></script>\n"
289 + "<script>log('end')</script>\n"
290 + "</html>";
291
292 getMockWebConnection().setResponse(new URL(URL_FIRST, "script1.js"), "log('deferred-1');");
293 getMockWebConnection().setResponse(new URL(URL_FIRST, "script2.js"), "log('deferred-2');");
294 getMockWebConnection().setResponse(new URL(URL_FIRST, "script3.js"), "log('deferred-3');");
295
296 loadPageVerifyTitle2(html);
297 }
298
299
300
301
302
303 @Test
304 @Alerts(DEFAULT = {"dcl listener added", "head-end", "end",
305 "deferred-2", "deferred-1", "deferred-3", "dcLoaded", "onload"},
306 CHROME = {"dcl listener added", "head-end", "end",
307 "deferred-1", "deferred-3", "dcLoaded", "deferred-2", "onload"},
308 EDGE = {"dcl listener added", "head-end", "end",
309 "deferred-1", "deferred-3", "dcLoaded", "deferred-2", "onload"})
310 @HtmlUnitNYI(CHROME = {"dcl listener added", "head-end", "end",
311 "deferred-1", "deferred-2", "deferred-3", "dcLoaded", "onload"},
312 EDGE = {"dcl listener added", "head-end", "end",
313 "deferred-1", "deferred-2", "deferred-3", "dcLoaded", "onload"},
314 FF = {"dcl listener added", "head-end", "end",
315 "deferred-1", "deferred-2", "deferred-3", "dcLoaded", "onload"},
316 FF_ESR = {"dcl listener added", "head-end", "end",
317 "deferred-1", "deferred-2", "deferred-3", "dcLoaded", "onload"})
318 public void deferDynamicExternal() throws Exception {
319 final String html = DOCTYPE_HTML
320 + "<html>\n"
321 + "<head>\n"
322 + " <script>\n"
323 + LOG_TITLE_FUNCTION
324 + " document.addEventListener('DOMContentLoaded', function(event) { log('dcLoaded') });\n"
325 + " log('dcl listener added')</script>"
326 + " </script>\n"
327 + " <script defer src='script1.js'></script>\n"
328 + " <script>\n"
329 + " head = document.getElementsByTagName('head')[0];\n"
330
331 + " script = document.createElement('script');\n"
332 + " script.setAttribute('defer', 'defer');\n"
333 + " script.setAttribute('src', 'script2.js');\n"
334 + " head.appendChild(script);\n"
335 + " </script>\n"
336 + " <script defer src='script3.js'></script>\n"
337 + " <script>log('head-end')</script>\n"
338 + "</head>\n"
339 + "<body onload='log(\"onload\")'>\n"
340 + " <div id='abc'>Test</div>\n"
341 + "</body>\n"
342 + "<script>log('end')</script>\n"
343 + "</html>";
344
345 getMockWebConnection().setResponse(new URL(URL_FIRST, "script1.js"), "log('deferred-1');");
346 getMockWebConnection().setResponse(new URL(URL_FIRST, "script2.js"), "log('deferred-2');");
347 getMockWebConnection().setResponse(new URL(URL_FIRST, "script3.js"), "log('deferred-3');");
348
349 loadPageVerifyTitle2(html);
350 }
351
352
353
354
355 @Test
356 @Alerts({"end", "s0 6", "5", "deferred-1", "deferred-2", "deferred-3", "onload"})
357 public void deferRemovesScript() throws Exception {
358 final String html = DOCTYPE_HTML
359 + "<html>\n"
360 + "<head>\n"
361 + " <script>\n"
362 + LOG_TITLE_FUNCTION
363 + " </script>\n"
364 + " <script defer id='s0' src='script0.js'></script>\n"
365 + " <script defer id='s1' src='script1.js'></script>\n"
366 + " <script defer id='s2' src='script2.js'></script>\n"
367 + " <script defer id='s3' src='script3.js'></script>\n"
368 + "</head>\n"
369 + "<body onload='log(\"onload\")'>\n"
370 + "</body>\n"
371 + "<script>log('end')</script>\n"
372 + "</html>";
373
374 getMockWebConnection().setResponse(new URL(URL_FIRST, "script0.js"),
375 "log('s0 ' + document.getElementsByTagName('script').length);\n"
376 + "var scr = document.getElementById('s3');\n"
377 + "scr.parentNode.removeChild(scr);\n"
378 + " log(document.getElementsByTagName('script').length);\n");
379 getMockWebConnection().setResponse(new URL(URL_FIRST, "script1.js"), "log('deferred-1');");
380 getMockWebConnection().setResponse(new URL(URL_FIRST, "script2.js"), "log('deferred-2');");
381 getMockWebConnection().setResponse(new URL(URL_FIRST, "script3.js"), "log('deferred-3');");
382
383 loadPageVerifyTitle2(html);
384 }
385
386
387
388
389
390 @Test
391 @Alerts({"false", "false"})
392 public void appendChild_newIdAndScriptAddedInOnce() throws Exception {
393 final String html = DOCTYPE_HTML
394 + "<html><body>\n"
395 + "<script>\n"
396 + LOG_TITLE_FUNCTION
397 + " var div1 = document.createElement('div');\n"
398 + " div1.id = 'div1';\n"
399 + " var script = document.createElement('script');\n"
400 + " script.text = 'log(document.getElementById(\"div1\") == null)';\n"
401 + " div1.appendChild(script);\n"
402 + " document.body.appendChild(div1);\n"
403 + " log(document.getElementById('div1') == null);\n"
404 + "</script>\n"
405 + "</body></html>";
406
407 loadPageVerifyTitle2(html);
408 }
409
410
411
412
413 @Test
414 @Alerts({"1", "2"})
415 public void executesMultipleTextNodes() throws Exception {
416 final String html = DOCTYPE_HTML
417 + "<html><body>\n"
418 + "<script>\n"
419 + LOG_TITLE_FUNCTION
420 + " var script = document.createElement('script');\n"
421 + " try {\n"
422 + " script.appendChild(document.createTextNode('log(\"1\");'));\n"
423 + " script.appendChild(document.createTextNode('log(\"2\");'));\n"
424 + " } catch(e) {\n"
425 + " script.text = 'log(\"1\");log(\"2\");';\n"
426 + " }\n"
427 + " document.body.appendChild(script);\n"
428 + "</script>\n"
429 + "</body></html>";
430
431 loadPageVerifyTitle2(html);
432 }
433
434
435
436
437 @Test
438 @Alerts("var x=1;x=2;")
439 public void getTextMultipleTextNodes() throws Exception {
440 final String html = DOCTYPE_HTML
441 + "<html><body>\n"
442 + "<script>\n"
443 + LOG_TITLE_FUNCTION
444 + " var script = document.createElement('script');\n"
445 + " try {\n"
446 + " script.appendChild(document.createTextNode('var x=1;'));\n;\n"
447 + " script.appendChild(document.createTextNode('x=2;'));\n;\n"
448 + " } catch(e) {\n"
449 + " script.text = 'var x=1;x=2;';\n;\n"
450 + " }\n"
451 + " document.body.appendChild(script);\n"
452 + " log(script.text);\n"
453 + "</script>\n"
454 + "</body></html>";
455
456 loadPageVerifyTitle2(html);
457 }
458
459
460
461
462 @Test
463 @Alerts("3")
464 public void setTextMultipleTextNodes() throws Exception {
465 final String html = DOCTYPE_HTML
466 + "<html><body>\n"
467 + "<script>\n"
468 + LOG_TITLE_FUNCTION
469 + " try {\n"
470 + " var script = document.createElement('script');\n"
471 + " script.appendChild(document.createTextNode('log(\"1\");'));\n"
472 + " script.appendChild(document.createTextNode('log(\"2\");'));\n"
473 + " script.text = 'log(\"3\");';\n"
474 + " document.body.appendChild(script);\n"
475 + " } catch(e) {logEx(e);}\n"
476 + "</script>\n"
477 + "</body></html>";
478
479 loadPageVerifyTitle2(html);
480 }
481
482
483
484
485
486 @Test
487 @Alerts({"1", "2", "3"})
488 public void settingSrcAttribute() throws Exception {
489 final String html = DOCTYPE_HTML
490 + "<html>\n"
491 + " <head>\n"
492 + " <script>\n"
493 + LOG_TITLE_FUNCTION
494 + " </script>\n"
495 + " <script id='a'></script>\n"
496 + " <script id='b'>log('1');</script>\n"
497 + " <script id='c' src='script2.js'></script>\n"
498 + " <script>\n"
499 + " function test() {\n"
500 + " document.getElementById('a').src = 'script3.js';\n"
501 + " document.getElementById('b').src = 'script4.js';\n"
502 + " document.getElementById('c').src = 'script5.js';\n"
503 + " }\n"
504 + " </script>\n"
505 + " </head>\n"
506 + " <body onload='test()'>\n"
507 + " test\n"
508 + " </body>\n"
509 + "</html>";
510
511 getMockWebConnection().setResponse(new URL(URL_FIRST, "script2.js"), "log(2);");
512 getMockWebConnection().setResponse(new URL(URL_FIRST, "script3.js"), "log(3);");
513 getMockWebConnection().setResponse(new URL(URL_FIRST, "script4.js"), "log(4);");
514 getMockWebConnection().setResponse(new URL(URL_FIRST, "script5.js"), "log(5);");
515
516 loadPage2(html);
517 verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
518 }
519
520
521
522
523 @Test
524 @Alerts({"s-x", "z"})
525 public void addEventListener_load() throws Exception {
526 final String html = DOCTYPE_HTML
527 + "<html><head>\n"
528 + "<script>\n"
529 + LOG_TITLE_FUNCTION
530 + " function test() {\n"
531 + " var s1 = document.createElement('script');\n"
532 + " s1.text = 'log(\"s-x\")';\n"
533 + " s1.addEventListener('load', function() {log('x')}, false);\n"
534 + " document.body.insertBefore(s1, document.body.firstChild);\n"
535 + " \n"
536 + " var s2 = document.createElement('script');\n"
537 + " s2.src = '//:';\n"
538 + " s2.addEventListener('load', function() {log('y')}, false);\n"
539 + " document.body.insertBefore(s2, document.body.firstChild);\n"
540 + " \n"
541 + " var s3 = document.createElement('script');\n"
542 + " s3.src = 'script.js';\n"
543 + " s3.addEventListener('load', function() {log('z')}, false);\n"
544 + " document.body.insertBefore(s3, document.body.firstChild);\n"
545 + " }\n"
546 + "</script>\n"
547 + "</head>\n"
548 + "<body onload='test()'></body>\n"
549 + "</html>";
550
551 getMockWebConnection().setDefaultResponse("", MimeType.TEXT_JAVASCRIPT);
552
553 loadPage2(html);
554 verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
555 }
556
557
558
559
560 @Test
561 @Alerts("load")
562 public void addEventListener_NoContent() throws Exception {
563 addEventListener(HttpStatus.NO_CONTENT_204);
564 }
565
566
567
568
569 @Test
570 @Alerts("error [object HTMLScriptElement]")
571 public void addEventListener_BadRequest() throws Exception {
572 addEventListener(HttpStatus.BAD_REQUEST_400);
573 }
574
575
576
577
578 @Test
579 @Alerts("error [object HTMLScriptElement]")
580 public void addEventListener_Forbidden() throws Exception {
581 addEventListener(HttpStatus.FORBIDDEN_403);
582 }
583
584
585
586
587 @Test
588 @Alerts("error [object HTMLScriptElement]")
589 public void addEventListener_NotFound() throws Exception {
590 addEventListener(HttpStatus.NOT_FOUND_404);
591 }
592
593
594
595
596 @Test
597 @Alerts("error [object HTMLScriptElement]")
598 public void addEventListener_MethodNotAllowed() throws Exception {
599 addEventListener(HttpStatus.METHOD_NOT_ALLOWED_405);
600 }
601
602
603
604
605 @Test
606 @Alerts("error [object HTMLScriptElement]")
607 public void addEventListener_NotAcceptable() throws Exception {
608 addEventListener(HttpStatus.NOT_ACCEPTABLE_406);
609 }
610
611
612
613
614 @Test
615 @Alerts("error [object HTMLScriptElement]")
616 public void addEventListener_ProxyAuthRequired() throws Exception {
617 addEventListener(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
618 }
619
620
621
622
623 @Test
624 @Alerts("error [object HTMLScriptElement]")
625 public void addEventListener_RequestTimeout() throws Exception {
626 addEventListener(HttpStatus.REQUEST_TIMEOUT_408);
627 }
628
629
630
631
632 @Test
633 @Alerts("error [object HTMLScriptElement]")
634 public void addEventListener_Conflict() throws Exception {
635 addEventListener(HttpStatus.CONFLICT_409);
636 }
637
638
639
640
641 @Test
642 @Alerts("error [object HTMLScriptElement]")
643 public void addEventListener_Gone() throws Exception {
644 addEventListener(HttpStatus.GONE_410);
645 }
646
647
648
649
650 @Test
651 @Alerts("error [object HTMLScriptElement]")
652 public void addEventListener_LengthRequired() throws Exception {
653 addEventListener(HttpStatus.LENGTH_REQUIRED_411);
654 }
655
656
657
658
659 @Test
660 @Alerts("error [object HTMLScriptElement]")
661 public void addEventListener_PreconditionFailed() throws Exception {
662 addEventListener(HttpStatus.PRECONDITION_FAILED_412);
663 }
664
665
666
667
668 @Test
669 @Alerts("error [object HTMLScriptElement]")
670 public void addEventListener_PayloadTooLarge() throws Exception {
671 addEventListener(HttpStatus.PAYLOAD_TOO_LARGE_413);
672 }
673
674
675
676
677 @Test
678 @Alerts("error [object HTMLScriptElement]")
679 public void addEventListener_UriTooLong() throws Exception {
680 addEventListener(HttpStatus.URI_TOO_LONG_414);
681 }
682
683
684
685
686 @Test
687 @Alerts("error [object HTMLScriptElement]")
688 public void addEventListener_UnsupportedMediaType() throws Exception {
689 addEventListener(HttpStatus.UNSUPPORTED_MEDIA_TYPE_415);
690 }
691
692
693
694
695 @Test
696 @Alerts("error [object HTMLScriptElement]")
697 public void addEventListener_RangeNotSatisfiable() throws Exception {
698 addEventListener(HttpStatus.RANGE_NOT_SATISFIABLE_416);
699 }
700
701
702
703
704 @Test
705 @Alerts("error [object HTMLScriptElement]")
706 public void addEventListener_ExpectationFailed() throws Exception {
707 addEventListener(HttpStatus.EXPECTATION_FAILED_417);
708 }
709
710
711
712
713 @Test
714 @Alerts("error [object HTMLScriptElement]")
715 public void addEventListener_ImaTeapot() throws Exception {
716 addEventListener(HttpStatus.IM_A_TEAPOT_418);
717 }
718
719
720
721
722 @Test
723 @Alerts("error [object HTMLScriptElement]")
724 public void addEventListener_EnhanceYourCalm() throws Exception {
725 addEventListener(HttpStatus.ENHANCE_YOUR_CALM_420);
726 }
727
728
729
730
731 @Test
732 @Alerts("error [object HTMLScriptElement]")
733 public void addEventListener_MisdirectedRequest() throws Exception {
734 addEventListener(HttpStatus.MISDIRECTED_REQUEST_421);
735 }
736
737
738
739
740 @Test
741 @Alerts("error [object HTMLScriptElement]")
742 public void addEventListener_UnprocessableEntity() throws Exception {
743 addEventListener(HttpStatus.UNPROCESSABLE_ENTITY_422);
744 }
745
746
747
748
749 @Test
750 @Alerts("error [object HTMLScriptElement]")
751 public void addEventListener_Locked() throws Exception {
752 addEventListener(HttpStatus.LOCKED_423);
753 }
754
755
756
757
758 @Test
759 @Alerts("error [object HTMLScriptElement]")
760 public void addEventListener_FailedDependency() throws Exception {
761 addEventListener(HttpStatus.FAILED_DEPENDENCY_424);
762 }
763
764
765
766
767 @Test
768 @Alerts("error [object HTMLScriptElement]")
769 public void addEventListener_UpgradeRequired() throws Exception {
770 addEventListener(HttpStatus.UPGRADE_REQUIRED_426);
771 }
772
773
774
775
776 @Test
777 @Alerts("error [object HTMLScriptElement]")
778 public void addEventListener_PreconditionRequired() throws Exception {
779 addEventListener(HttpStatus.PRECONDITION_REQUIRED_428);
780 }
781
782
783
784
785 @Test
786 @Alerts("error [object HTMLScriptElement]")
787 public void addEventListener_TooManyRedirects() throws Exception {
788 addEventListener(HttpStatus.TOO_MANY_REQUESTS_429);
789 }
790
791
792
793
794 @Test
795 @Alerts("error [object HTMLScriptElement]")
796 public void addEventListener_RequestHeaderFieldsTooLarge() throws Exception {
797 addEventListener(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431);
798 }
799
800
801
802
803 @Test
804 @Alerts("error [object HTMLScriptElement]")
805 public void addEventListener_UnavailableForLegalReasons() throws Exception {
806 addEventListener(HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS_451);
807 }
808
809
810
811
812 @Test
813 @Alerts("error [object HTMLScriptElement]")
814 public void addEventListener_InternalServerError() throws Exception {
815 addEventListener(HttpStatus.INTERNAL_SERVER_ERROR_500);
816 }
817
818
819
820
821 @Test
822 @Alerts("error [object HTMLScriptElement]")
823 public void addEventListener_NotImplemented() throws Exception {
824 addEventListener(HttpStatus.NOT_IMPLEMENTED_501);
825 }
826
827
828
829
830 @Test
831 @Alerts("error [object HTMLScriptElement]")
832 public void addEventListener_BadGateway() throws Exception {
833 addEventListener(HttpStatus.BAD_GATEWAY_502);
834 }
835
836
837
838
839 @Test
840 @Alerts("error [object HTMLScriptElement]")
841 public void addEventListener_ServiceUnavailable() throws Exception {
842 addEventListener(HttpStatus.SERVICE_UNAVAILABLE_503);
843 }
844
845
846
847
848 @Test
849 @Alerts("error [object HTMLScriptElement]")
850 public void addEventListener_GatewayTimeout() throws Exception {
851 addEventListener(HttpStatus.GATEWAY_TIMEOUT_504);
852 }
853
854
855
856
857 @Test
858 @Alerts("error [object HTMLScriptElement]")
859 public void addEventListener_HttpVersionNotSupported() throws Exception {
860 addEventListener(HttpStatus.HTTP_VERSION_NOT_SUPPORTED_505);
861 }
862
863
864
865
866 @Test
867 @Alerts("error [object HTMLScriptElement]")
868 public void addEventListener_InsufficientStrorage() throws Exception {
869 addEventListener(HttpStatus.INSUFFICIENT_STORAGE_507);
870 }
871
872
873
874
875 @Test
876 @Alerts("error [object HTMLScriptElement]")
877 public void addEventListener_LoopDetected() throws Exception {
878 addEventListener(HttpStatus.LOOP_DETECTED_508);
879 }
880
881
882
883
884 @Test
885 @Alerts("error [object HTMLScriptElement]")
886 public void addEventListener_NotExtended() throws Exception {
887 addEventListener(HttpStatus.NOT_EXTENDED_510);
888 }
889
890
891
892
893 @Test
894 @Alerts("error [object HTMLScriptElement]")
895 public void addEventListener_NetworkAuthenticationRequired() throws Exception {
896 addEventListener(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED_511);
897 }
898
899 private void addEventListener(final int statusCode) throws Exception {
900
901 final URL scriptUrl = new URL(URL_SECOND, "" + System.currentTimeMillis() + ".js");
902
903 final String html = DOCTYPE_HTML
904 + "<html><head>\n"
905 + "<script>\n"
906 + LOG_TITLE_FUNCTION
907 + " function test() {\n"
908 + " var s1 = document.createElement('script');\n"
909 + " s1.src = '" + scriptUrl + "';\n"
910 + " s1.addEventListener('load', function() { log('load'); }, false);\n"
911 + " s1.addEventListener('error', function(event) { log(event.type + ' ' + event.target); }, false);\n"
912 + " document.body.insertBefore(s1, document.body.firstChild);\n"
913 + " }\n"
914 + "</script>\n"
915 + "</head>\n"
916 + "<body onload='test()'></body>\n"
917 + "</html>";
918
919 getMockWebConnection().setResponse(scriptUrl, (String) null,
920 statusCode, "test", MimeType.TEXT_JAVASCRIPT, null);
921 loadPageVerifyTitle2(html);
922 }
923
924
925
926
927
928 @Test
929 public void badSrcUrl() throws Exception {
930 final String html = DOCTYPE_HTML
931 + "<html><head>\n"
932 + "<script src='http://'>log(1)</script>\n"
933 + "</head><body></body></html>";
934
935 loadPageVerifyTitle2(html);
936 }
937
938
939
940
941
942
943 @Test
944 public void invalidJQuerySrcAttribute() throws Exception {
945 loadPage2(DOCTYPE_HTML + "<html><body><script src='//:'></script></body></html>");
946 }
947
948
949
950
951 @Test
952 @Alerts({"loaded", "§§URL§§abcd"})
953 public void lineBreaksInUrl() throws Exception {
954 final String html = DOCTYPE_HTML
955 + "<html><head>\n"
956 + " <script>\n"
957 + LOG_TITLE_FUNCTION_NORMALIZE
958 + " </script>\n"
959 + " <script id='myScript' src='" + URL_SECOND + "a\rb\nc\r\nd'></script>\n"
960 + "</head>\n"
961 + "<body onload='log(document.getElementById(\"myScript\").src);'>Test</body>\n"
962 + "</html>";
963
964 getMockWebConnection().setResponse(new URL(URL_SECOND, "abcd"), "log('loaded')");
965 expandExpectedAlertsVariables(URL_SECOND);
966
967 loadPageVerifyTitle2(html);
968 }
969
970
971
972
973 @Test
974 @Alerts({"\u0623\u0647\u0644\u0627\u064b\u0623\u0647\u0644\u0627"
975 + "\u064b\u0623\u0647\u0644\u0627\u064b\u0623\u0647\u0644\u0627\u064b", "§§URL§§"})
976 public void incorrectCharset() throws Exception {
977 final String html = DOCTYPE_HTML
978 + "<html><head>\n"
979 + " <script>\n"
980 + LOG_TITLE_FUNCTION_NORMALIZE
981 + " </script>\n"
982 + " <script id='myScript' src='" + URL_SECOND + "' charset='" + ISO_8859_1 + "'></script>\n"
983 + "</head>\n"
984 + "<body onload='log(document.getElementById(\"myScript\").src);'></body>\n"
985 + "</html>";
986
987 final String script = new String(ByteOrderMark.UTF_8.getBytes())
988 + "log('" + "\u0623\u0647\u0644\u0627\u064b\u0623\u0647\u0644\u0627"
989 + "\u064b\u0623\u0647\u0644\u0627\u064b\u0623\u0647\u0644\u0627\u064b" + "');";
990 getMockWebConnection().setResponse(URL_SECOND, script, MimeType.TEXT_JAVASCRIPT, UTF_8);
991 expandExpectedAlertsVariables(URL_SECOND);
992
993 loadPageVerifyTitle2(html);
994 }
995
996
997
998
999 @Test
1000 @Alerts({"onLoad", "body onLoad"})
1001 public void onLoad() throws Exception {
1002 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "");
1003 onLoadOnError("src='simple.js' type='text/javascript'");
1004 }
1005
1006
1007
1008
1009 @Test
1010 @Alerts({"onLoad", "body onLoad"})
1011 public void onLoadTypeWhitespace() throws Exception {
1012 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "");
1013 onLoadOnError("src='simple.js' type='\t text/javascript '");
1014 }
1015
1016
1017
1018
1019 @Test
1020 @Alerts({"onError", "body onLoad"})
1021 public void onError() throws Exception {
1022 onLoadOnError("src='unknown.js' type='text/javascript'");
1023 }
1024
1025
1026
1027
1028 @Test
1029 @Alerts({"onError", "body onLoad"})
1030 public void onLoadOnErrorWithoutType() throws Exception {
1031 onLoadOnError("src='unknown.js'");
1032 }
1033
1034 private void onLoadOnError(final String attribs) throws Exception {
1035 final String html = DOCTYPE_HTML
1036 + "<html>\n"
1037 + "<head>\n"
1038 + " <script>\n"
1039 + LOG_TITLE_FUNCTION
1040 + " </script>\n"
1041 + " <script " + attribs
1042 + " onload='log(\"onLoad\")' onerror='log(\"onError\")'></script>\n"
1043 + "</head>\n"
1044 + "<body onload='log(\"body onLoad\")'>\n"
1045 + "</body>\n"
1046 + "</html>";
1047 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
1048
1049 loadPageVerifyTitle2(html);
1050 }
1051
1052
1053
1054
1055 @Test
1056 @Alerts({"from script", "onLoad [object Event]"})
1057 public void onLoadDynamic() throws Exception {
1058 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "log('from script');");
1059 final String html = DOCTYPE_HTML
1060 + "<html>\n"
1061 + "<head>\n"
1062 + " <script>\n"
1063 + LOG_TITLE_FUNCTION
1064 + " function test() {\n"
1065 + " var dynScript = document.createElement('script');\n"
1066 + " dynScript.type = 'text/javascript';\n"
1067 + " dynScript.onload = function (e) { log(\"onLoad \" + e) };\n"
1068 + " document.head.appendChild(dynScript);\n"
1069 + " dynScript.src = 'simple.js';"
1070 + " }\n"
1071 + " </script>\n"
1072 + "</head>\n"
1073 + "<body onload='test()'></body>\n"
1074 + "</html>";
1075
1076 final WebDriver driver = loadPage2(html);
1077 verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts());
1078 }
1079
1080
1081
1082
1083 @Test
1084 @Alerts("[object HTMLScriptElement]")
1085 public void currentScriptInline() throws Exception {
1086 final String html = DOCTYPE_HTML
1087 + "<html>\n"
1088 + "<head>\n"
1089 + " <script id='tester'>\n"
1090 + LOG_TITLE_FUNCTION
1091 + " log(document.currentScript);\n"
1092 + " </script>\n"
1093 + "</head>\n"
1094 + "<body>\n"
1095 + "</body>\n"
1096 + "</html>";
1097
1098 loadPageVerifyTitle2(html);
1099 }
1100
1101
1102
1103
1104 @Test
1105 @Alerts("null")
1106 public void currentScriptFunction() throws Exception {
1107 final String html = DOCTYPE_HTML
1108 + "<html>\n"
1109 + "<head>\n"
1110 + " <script id='tester'>\n"
1111 + LOG_TITLE_FUNCTION
1112 + " function test() {\n"
1113 + " log(document.currentScript);\n"
1114 + " }\n"
1115 + "</script>\n"
1116 + "</head>\n"
1117 + "<body onload='test()'>\n"
1118 + "</body>\n"
1119 + "</html>";
1120
1121 loadPageVerifyTitle2(html);
1122 }
1123
1124
1125
1126
1127 @Test
1128 @Alerts("[object HTMLScriptElement]")
1129 public void currentScriptExternal() throws Exception {
1130 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "log(document.currentScript);");
1131 final String html = DOCTYPE_HTML
1132 + "<html>\n"
1133 + "<head>\n"
1134 + " <script>\n"
1135 + LOG_TITLE_FUNCTION
1136 + " </script>\n"
1137 + " <script id='tester' src='simple.js' type='text/javascript'></script>\n"
1138 + "</head>\n"
1139 + "<body>\n"
1140 + "</body>\n"
1141 + "</html>";
1142
1143 loadPage2(html);
1144 verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
1145 }
1146
1147
1148
1149
1150 @Test
1151 @Alerts("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
1152 + "21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39")
1153 public void scriptExecutionOrder() throws Exception {
1154 final StringBuilder html = new StringBuilder();
1155 html.append(DOCTYPE_HTML + "<html>\n<head>\n");
1156 int i = 0;
1157 for ( ; i < 20; i++) {
1158 html.append(" <script type='text/javascript'>document.title += ' ")
1159 .append(Integer.toString(i))
1160 .append("'</script>\n");
1161 }
1162 html.append("</head>\n<body>\n");
1163 for ( ; i < 40; i++) {
1164 html.append(" <script type='text/javascript'>document.title += ' ")
1165 .append(Integer.toString(i))
1166 .append("'</script>\n");
1167 }
1168 html.append("</body>\n</html>");
1169
1170 final WebDriver driver = loadPage2(html.toString());
1171 assertTitle(driver, getExpectedAlerts()[0]);
1172 }
1173
1174
1175
1176
1177
1178 @Test
1179 @Alerts("§§URL§§index.html?test")
1180 public void refererHeader() throws Exception {
1181 final String firstContent = DOCTYPE_HTML
1182 + "<html><head><title>Page A</title></head>\n"
1183 + "<body><script src='" + URL_SECOND + "'/></body>\n"
1184 + "</html>";
1185
1186 final String secondContent = "var i = 7;";
1187
1188 expandExpectedAlertsVariables(URL_FIRST);
1189
1190 final URL indexUrl = new URL(URL_FIRST.toString() + "index.html");
1191
1192 getMockWebConnection().setResponse(indexUrl, firstContent);
1193 getMockWebConnection().setResponse(URL_SECOND, secondContent);
1194
1195 loadPage2(firstContent, new URL(URL_FIRST.toString() + "index.html?test#ref"));
1196
1197 assertEquals(2, getMockWebConnection().getRequestCount());
1198
1199 final Map<String, String> lastAdditionalHeaders = getMockWebConnection().getLastAdditionalHeaders();
1200 assertEquals(getExpectedAlerts()[0], lastAdditionalHeaders.get(HttpHeader.REFERER));
1201 }
1202
1203
1204
1205
1206
1207 @Test
1208 @Alerts({"loaded", "§§URL§§"})
1209 public void whitespaceInSrc() throws Exception {
1210 final String html = DOCTYPE_HTML
1211 + "<html><head>"
1212 + " <script>" + LOG_TITLE_FUNCTION_NORMALIZE + "</script>"
1213 + "<script id='myScript' src=' " + URL_SECOND + " '></script></head>"
1214 + "<body onload='log(document.getElementById(\"myScript\").src);'>abc</body></html>";
1215
1216 final String js = "log('loaded')";
1217 getMockWebConnection().setResponse(URL_SECOND, js);
1218 expandExpectedAlertsVariables(URL_SECOND);
1219
1220 loadPageVerifyTitle2(html);
1221 }
1222
1223
1224
1225
1226
1227 @Test
1228 @Alerts({"loaded", "§§URL§§"})
1229 public void controlCharsInSrc() throws Exception {
1230 final String html = DOCTYPE_HTML
1231 + "<html><head>"
1232 + " <script>" + LOG_TITLE_FUNCTION_NORMALIZE + "</script>"
1233 + "<script id='myScript' src=' " + URL_SECOND + "\u001d'></script></head>"
1234 + "<body onload='log(document.getElementById(\"myScript\").src);'>abc</body></html>";
1235
1236 final String js = "log('loaded')";
1237 getMockWebConnection().setResponse(URL_SECOND, js);
1238 expandExpectedAlertsVariables(URL_SECOND);
1239
1240 loadPageVerifyTitle2(html);
1241 }
1242
1243
1244
1245
1246
1247 @Test
1248 @Alerts({"loaded", "§§URL§§"})
1249 public void tabCharInSrc() throws Exception {
1250 String url = URL_SECOND.toExternalForm();
1251 url = url.replace("http", "http\t");
1252
1253 final String html = DOCTYPE_HTML
1254 + "<html><head>"
1255 + " <script>" + LOG_TITLE_FUNCTION_NORMALIZE + "</script>"
1256 + "<script id='myScript' src=' " + url + "\u001d'></script></head>"
1257 + "<body onload='log(document.getElementById(\"myScript\").src);'>abc</body></html>";
1258
1259 final String js = "log('loaded')";
1260 getMockWebConnection().setResponse(URL_SECOND, js);
1261 expandExpectedAlertsVariables(URL_SECOND);
1262
1263 loadPageVerifyTitle2(html);
1264 }
1265
1266
1267
1268
1269
1270 @Test
1271 public void emptySrc() throws Exception {
1272 final String html1 = DOCTYPE_HTML + "<html><head><script src=''></script></head><body>abc</body></html>";
1273 final String html2 = DOCTYPE_HTML + "<html><head><script src=' '></script></head><body>abc</body></html>";
1274
1275 loadPageWithAlerts2(html1);
1276 loadPageWithAlerts2(html2);
1277 }
1278
1279
1280
1281
1282
1283
1284 @Test
1285 public void noContent() throws Exception {
1286 final String html = DOCTYPE_HTML + "<html><body><script src='" + URL_SECOND + "'/></body></html>";
1287
1288 final ArrayList<NameValuePair> headers = new ArrayList<>();
1289 getMockWebConnection().setResponse(URL_SECOND, (String) null,
1290 HttpStatus.NO_CONTENT_204, HttpStatus.NO_CONTENT_204_MSG,
1291 MimeType.TEXT_JAVASCRIPT,
1292 headers);
1293
1294 loadPageWithAlerts2(html);
1295 }
1296
1297
1298
1299
1300 @Test
1301 @Alerts("script")
1302 public void srcAndContent() throws Exception {
1303 final String html = DOCTYPE_HTML
1304 + "<html>\n"
1305 + " <head>\n"
1306 + " <script src='foo.js'>window.document.title += 'content' + '\\u00a7';</script>\n";
1307
1308 final String js = "window.document.title += 'script' + '\\u00a7';";
1309
1310 getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
1311
1312 loadPageVerifyTitle2(html);
1313 }
1314
1315
1316
1317
1318 @Test
1319 public void emptySrcAndContent() throws Exception {
1320 final String html = DOCTYPE_HTML
1321 + "<html>\n"
1322 + " <head>\n"
1323 + " <script src=''>window.document.title += 'content' + '\\u00a7';</script>\n";
1324
1325 final String js = "window.document.title += 'script' + '\\u00a7';";
1326
1327 getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
1328
1329 loadPageVerifyTitle2(html);
1330 }
1331
1332
1333
1334
1335 @Test
1336 public void blankSrcAndContent() throws Exception {
1337 final String html = DOCTYPE_HTML
1338 + "<html>\n"
1339 + " <head>\n"
1340 + " <script src=' '>window.document.title += 'content' + '\\u00a7';</script>\n";
1341
1342 final String js = "window.document.title += 'script' + '\\u00a7';";
1343
1344 getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
1345
1346 loadPageVerifyTitle2(html);
1347 }
1348
1349
1350
1351
1352 @Test
1353 public void attribSrcAndContent() throws Exception {
1354 final String html = DOCTYPE_HTML
1355 + "<html>\n"
1356 + " <head>\n"
1357 + " <script src>window.document.title += 'content' + '\\u00a7';</script>\n";
1358
1359 final String js = "window.document.title += 'script' + '\\u00a7';";
1360
1361 getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
1362
1363 loadPageVerifyTitle2(html);
1364 }
1365
1366
1367
1368
1369
1370 @Test
1371 @Alerts({"first script", "second script"})
1372 public void content() throws Exception {
1373 final String html = DOCTYPE_HTML
1374 + "<html>\n"
1375 + " <head>\n"
1376 + " <script src='foo.js'>window.document.title += 'content' + '\\u00a7';</script>\n"
1377 + " <script>window.document.title += 'second script' + '\\u00a7';</script>\n";
1378
1379 final String js = "window.document.title += 'first script' + '\\u00a7';";
1380
1381 getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
1382
1383 loadPageVerifyTitle2(html);
1384 }
1385 }