1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.WebDriver;
24 import org.openqa.selenium.WebElement;
25
26
27
28
29
30
31
32
33
34 @RunWith(BrowserRunner.class)
35 public class HtmlCheckBoxInput2Test extends WebDriverTestCase {
36
37
38
39
40 @Test
41 @Alerts({"true", "true", "true"})
42 public void checked_appendChild_docFragment() throws Exception {
43 performTest(true, true, false, true, false);
44 }
45
46
47
48
49 @Test
50 @Alerts({"false", "false", "false"})
51 public void notchecked_appendChild_docFragment() throws Exception {
52 performTest(false, true, false, true, false);
53 }
54
55
56
57
58 @Test
59 @Alerts({"true", "true", "true"})
60 public void checked_insertBefore_docFragment() throws Exception {
61 performTest(true, false, false, true, false);
62 }
63
64
65
66
67 @Test
68 @Alerts({"false", "false", "false"})
69 public void notchecked_insertBefore_docFragment() throws Exception {
70 performTest(false, false, false, true, false);
71 }
72
73
74
75
76 @Test
77 @Alerts({"true", "true", "true"})
78 public void checked_appendChild_fromHtml_docFragment() throws Exception {
79 performTest(true, true, true, true, false);
80 }
81
82
83
84
85 @Test
86 @Alerts({"false", "false", "false"})
87 public void notchecked_appendChild_fromHtml_docFragment() throws Exception {
88 performTest(false, true, true, true, false);
89 }
90
91
92
93
94 @Test
95 @Alerts({"true", "true", "true"})
96 public void checked_insertBefore_fromHtml_docFragment() throws Exception {
97 performTest(true, false, true, true, false);
98 }
99
100
101
102
103 @Test
104 @Alerts({"false", "false", "false"})
105 public void notchecked_insertBefore_fromHtml_docFragment() throws Exception {
106 performTest(false, false, true, true, false);
107 }
108
109
110
111
112 @Test
113 @Alerts({"true", "true", "true"})
114 public void checked_appendChild_docFragment_cloneNode() throws Exception {
115 performTest(true, true, false, true, true);
116 }
117
118
119
120
121 @Test
122 @Alerts({"false", "false", "false"})
123 public void notchecked_appendChild_docFragment_cloneNode() throws Exception {
124 performTest(false, true, false, true, true);
125 }
126
127
128
129
130 @Test
131 @Alerts({"true", "true", "true"})
132 public void checked_insertBefore_docFragment_cloneNode() throws Exception {
133 performTest(true, false, false, true, true);
134 }
135
136
137
138
139 @Test
140 @Alerts({"false", "false", "false"})
141 public void notchecked_insertBefore_docFragment_cloneNode() throws Exception {
142 performTest(false, false, false, true, true);
143 }
144
145
146
147
148 @Test
149 @Alerts({"true", "true", "true"})
150 public void checked_appendChild_fromHtml_docFragment_cloneNode() throws Exception {
151 performTest(true, true, true, true, true);
152 }
153
154
155
156
157 @Test
158 @Alerts({"false", "false", "false"})
159 public void notchecked_appendChild_fromHtml_docFragment_cloneNode() throws Exception {
160 performTest(false, true, true, true, true);
161 }
162
163
164
165
166 @Test
167 @Alerts({"true", "true", "true"})
168 public void checked_insertBefore_fromHtml_docFragment_cloneNode() throws Exception {
169 performTest(true, false, true, true, true);
170 }
171
172
173
174
175 @Test
176 @Alerts({"false", "false", "false"})
177 public void notchecked_insertBefore_fromHtml_docFragment_cloneNode() throws Exception {
178 performTest(false, false, true, true, true);
179 }
180
181
182
183
184 @Test
185 @Alerts({"true", "true", "true", "true", "true", "true"})
186 public void checked_appendChild() throws Exception {
187 performTest(true, true, false, false, false);
188 }
189
190
191
192
193 @Test
194 @Alerts({"false", "false", "false", "true", "true", "true"})
195 public void notchecked_appendChild() throws Exception {
196 performTest(false, true, false, false, false);
197 }
198
199
200
201
202 @Test
203 @Alerts({"true", "true", "true", "true", "true", "true"})
204 public void checked_insertBefore() throws Exception {
205 performTest(true, false, false, false, false);
206 }
207
208
209
210
211 @Test
212 @Alerts({"false", "false", "false", "true", "true", "true"})
213 public void notchecked_insertBefore() throws Exception {
214 performTest(false, false, false, false, false);
215 }
216
217
218
219
220 @Test
221 @Alerts({"true", "true", "true", "true", "true", "true"})
222 public void checked_appendChild_fromHtml() throws Exception {
223 performTest(true, true, true, false, false);
224 }
225
226
227
228
229 @Test
230 @Alerts({"false", "false", "false", "true", "true", "true"})
231 public void notchecked_appendChild_fromHtml() throws Exception {
232 performTest(false, true, true, false, false);
233 }
234
235
236
237
238 @Test
239 @Alerts({"true", "true", "true", "true", "true", "true"})
240 public void checked_insertBefore_fromHtml() throws Exception {
241 performTest(true, false, true, false, false);
242 }
243
244
245
246
247 @Test
248 @Alerts({"false", "false", "false", "true", "true", "true"})
249 public void notchecked_insertBefore_fromHtml() throws Exception {
250 performTest(false, false, true, false, false);
251 }
252
253
254
255
256 @Test
257 @Alerts({"true", "true", "true", "true", "true", "true"})
258 public void checked_appendChild_cloneNode() throws Exception {
259 performTest(true, true, false, false, true);
260 }
261
262
263
264
265 @Test
266 @Alerts({"false", "false", "false", "true", "true", "true"})
267 public void notchecked_appendChild_cloneNode() throws Exception {
268 performTest(false, true, false, false, true);
269 }
270
271
272
273
274 @Test
275 @Alerts({"true", "true", "true", "true", "true", "true"})
276 public void checked_insertBefore_cloneNode() throws Exception {
277 performTest(true, false, false, false, true);
278 }
279
280
281
282
283 @Test
284 @Alerts({"false", "false", "false", "true", "true", "true"})
285 public void notchecked_insertBefore_cloneNode() throws Exception {
286 performTest(false, false, false, false, true);
287 }
288
289
290
291
292 @Test
293 @Alerts({"true", "true", "true", "true", "true", "true"})
294 public void checked_appendChild_fromHtml_cloneNode() throws Exception {
295 performTest(true, true, true, false, true);
296 }
297
298
299
300
301 @Test
302 @Alerts({"false", "false", "false", "true", "true", "true"})
303 public void notchecked_appendChild_fromHtml_cloneNode() throws Exception {
304 performTest(false, true, true, false, true);
305 }
306
307
308
309
310 @Test
311 @Alerts({"true", "true", "true", "true", "true", "true"})
312 public void checked_insertBefore_fromHtml_cloneNode() throws Exception {
313 performTest(true, false, true, false, true);
314 }
315
316
317
318
319 @Test
320 @Alerts({"false", "false", "false", "true", "true", "true"})
321 public void notchecked_insertBefore_fromHtml_cloneNode() throws Exception {
322 performTest(false, false, true, false, true);
323 }
324
325 private void performTest(final boolean checked,
326 final boolean appendChild,
327 final boolean fromHtml,
328 final boolean useFragment,
329 boolean cloneNode) throws Exception {
330 String html = DOCTYPE_HTML
331 + "<html>\n"
332 + "<head>\n"
333 + " <script>\n"
334 + LOG_TITLE_FUNCTION
335 + " function test() {\n";
336 if (fromHtml) {
337 html = html
338 + " var builder = document.createElement('div');\n"
339 + " builder.innerHTML = '<input type=\"checkbox\"";
340 if (checked) {
341 html = html + " checked";
342 }
343 html = html + ">';\n"
344 + " var input = builder.firstChild;\n";
345 }
346 else {
347 html = html
348 + " var input = document.createElement('input');\n"
349 + " input.type = 'checkbox';\n";
350 if (checked) {
351 html = html + " input.checked = true;\n";
352 }
353 }
354
355 if (cloneNode && !useFragment) {
356 html = html
357 + " input=input.cloneNode(true);\n";
358 cloneNode = false;
359 }
360 html = html
361 + " log(input.checked);\n"
362
363 + " var parent=document.getElementById('myDiv');\n"
364 + " var after=document.getElementById('divAfter');\n";
365 if (useFragment) {
366 html = html
367 + " var appendix=document.createDocumentFragment();\n"
368 + " appendix.appendChild(input);\n"
369 + " log(input.checked);\n";
370 }
371 else {
372 html = html
373 + " var appendix=input;\n";
374 }
375 if (appendChild) {
376 if (cloneNode) {
377 html = html + " parent.appendChild(appendix.cloneNode(true));\n";
378 }
379 else {
380 html = html + " parent.appendChild(appendix);\n";
381 }
382 }
383 else {
384 if (cloneNode) {
385 html = html + " parent.insertBefore(appendix.cloneNode(true), after);\n";
386 }
387 else {
388 html = html + " parent.insertBefore(appendix, after);\n";
389 }
390 }
391 html = html
392 + " input = parent.getElementsByTagName('input')[0];\n"
393 + " log(input.checked);\n";
394 if (!useFragment) {
395 html = html
396 + " parent.removeChild(input);\n"
397 + " log(input.checked);\n"
398 + "\n"
399 + " input.defaultChecked = true;\n"
400 + " log(input.checked);\n"
401 + " parent.appendChild(input);\n"
402 + " log(input.checked);\n"
403 + " parent.removeChild(input);\n"
404 + " log(input.checked);\n";
405 }
406 html = html
407 + " }\n"
408 + " </script>\n"
409 + "</head><body onload='test()'>\n"
410 + " <form><div id='myDiv'><div id='divAfter'></div></div></form>\n"
411 + "</body></html>";
412
413 loadPageVerifyTitle2(html);
414 }
415
416
417
418
419 @Test
420 @Alerts({"true-true", "true-true", "false-false", "false-false", "true-true", "false-false"})
421 public void defaultChecked() throws Exception {
422 final String html = DOCTYPE_HTML
423 + "<html>\n"
424 + "<head>\n"
425 + " <script>\n"
426 + LOG_TITLE_FUNCTION
427 + " function test() {\n"
428 + " chkbox = document.getElementById('chkboxChecked');\n"
429 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
430 + " chkbox.defaultChecked = true;\n"
431 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
432 + " chkbox.defaultChecked = false;\n"
433 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
434
435 + " chkbox = document.getElementById('chkboxNotChecked');\n"
436 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
437 + " chkbox.defaultChecked = true;\n"
438 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
439 + " chkbox.defaultChecked = false;\n"
440 + " log(chkbox.checked + '-' + chkbox.defaultChecked);\n"
441 + " }\n"
442 + " </script>\n"
443 + "</head><body onload='test()'>\n"
444 + " <form>\n"
445 + " <input type='checkbox' id='chkboxChecked' checked>\n"
446 + " <input type='checkbox' id='chkboxNotChecked'>\n"
447 + " </form>\n"
448 + "</body></html>";
449
450 loadPageVerifyTitle2(html);
451 }
452
453
454
455
456 @Test
457 @Alerts({"foo", "change"})
458 public void onchangeFires() throws Exception {
459 final String html = DOCTYPE_HTML
460 + "<html><head><title>foo</title>\n"
461 + "<script>\n"
462 + LOG_TEXTAREA_FUNCTION
463 + "</script>\n"
464 + "</head><body>\n"
465 + "<form>\n"
466 + " <input type='checkbox' id='chkbox' onchange='log(\"foo\");log(event.type);'>\n"
467 + "</form>\n"
468 + LOG_TEXTAREA
469 + "</body></html>";
470
471 final WebDriver driver = loadPage2(html);
472 driver.findElement(By.id("chkbox")).click();
473
474 verifyTextArea2(driver, getExpectedAlerts());
475 }
476
477
478
479
480 @Test
481 @Alerts({"onchange change", "onblur blur"})
482 public void onchangeFires2() throws Exception {
483 final String html = DOCTYPE_HTML
484 + "<html><head><title>foo</title>\n"
485 + "<script>\n"
486 + LOG_TEXTAREA_FUNCTION
487 + "</script>\n"
488 + "</head><body>\n"
489 + "<form>\n"
490 + "<input type='checkbox' id='chkbox'"
491 + " onChange='log(\"onchange \" + event.type);'"
492 + " onBlur='log(\"onblur \" + event.type);'"
493 + ">\n"
494 + "<input type='checkbox' id='chkbox2'>\n"
495 + "</form>\n"
496 + LOG_TEXTAREA
497 + "</body></html>";
498
499 final WebDriver driver = loadPage2(html);
500 driver.findElement(By.id("chkbox")).click();
501 driver.findElement(By.id("chkbox2")).click();
502
503 verifyTextArea2(driver, getExpectedAlerts());
504 }
505
506
507
508
509 @Test
510 @Alerts("Second")
511 public void setChecked() throws Exception {
512 final String firstHtml = DOCTYPE_HTML
513 + "<html><head><title>First</title></head><body>\n"
514 + "<form>\n"
515 + "<input id='myCheckbox' type='checkbox' onchange=\"window.location.href='" + URL_SECOND + "'\">\n"
516 + "</form>\n"
517 + "</body></html>";
518 final String secondHtml = DOCTYPE_HTML
519 + "<html><head><title>Second</title></head><body></body></html>";
520
521 getMockWebConnection().setDefaultResponse(secondHtml);
522 final WebDriver driver = loadPage2(firstHtml);
523
524 driver.findElement(By.id("myCheckbox")).click();
525 assertTitle(driver, getExpectedAlerts()[0]);
526 }
527
528
529
530
531 @Test
532 @Alerts("Second")
533 public void setChecked2() throws Exception {
534 final String firstHtml = DOCTYPE_HTML
535 + "<html><head><title>First</title></head><body>\n"
536 + "<form>\n"
537 + "<input id='myCheckbox' type='checkbox' onchange=\"window.location.href='" + URL_SECOND + "'\">\n"
538 + "<input id='myInput' type='text'>\n"
539 + "</form>\n"
540 + "</body></html>";
541 final String secondHtml = DOCTYPE_HTML
542 + "<html><head><title>Second</title></head><body></body></html>";
543
544 getMockWebConnection().setDefaultResponse(secondHtml);
545 final WebDriver driver = loadPage2(firstHtml);
546
547 driver.findElement(By.id("myCheckbox")).click();
548 assertTitle(driver, getExpectedAlerts()[0]);
549 }
550
551
552
553
554 @Test
555 public void preventDefault() throws Exception {
556 final String html = DOCTYPE_HTML
557 + "<html><head><script>\n"
558 + " function handler(e) {\n"
559 + " if (e)\n"
560 + " e.preventDefault();\n"
561 + " else\n"
562 + " return false;\n"
563 + " }\n"
564 + " function init() {\n"
565 + " document.getElementById('checkbox1').onclick = handler;\n"
566 + " }\n"
567 + "</script></head>\n"
568 + "<body onload='init()'>\n"
569 + "<input type='checkbox' id='checkbox1'/>\n"
570 + "</body></html>";
571
572 final WebDriver driver = loadPage2(html);
573 final WebElement checkbox = driver.findElement(By.id("checkbox1"));
574 checkbox.click();
575 assertFalse(checkbox.isSelected());
576 }
577
578
579
580
581
582
583 @Test
584 public void defaultState() throws Exception {
585 final String html = DOCTYPE_HTML
586 + "<html><head><title>foo</title></head><body>\n"
587 + "<form id='form1'>\n"
588 + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
589 + "</form></body></html>";
590 final WebDriver driver = loadPage2(html);
591 final WebElement checkbox = driver.findElement(By.id("checkbox"));
592 assertFalse(checkbox.isSelected());
593 }
594
595
596
597
598 @Test
599 @Alerts({"on-", "on-", "on-", "on-"})
600 public void defaultValues() throws Exception {
601 final String html = DOCTYPE_HTML
602 + "<html><head>\n"
603 + "<script>\n"
604 + LOG_TITLE_FUNCTION
605 + " function test() {\n"
606 + " var input = document.getElementById('chkbox1');\n"
607 + " log(input.value + '-' + input.defaultValue);\n"
608
609 + " input = document.getElementById('chkbox2');\n"
610 + " log(input.value + '-' + input.defaultValue);\n"
611
612 + " input = document.createElement('input');\n"
613 + " input.type = 'checkbox';\n"
614 + " log(input.value + '-' + input.defaultValue);\n"
615
616 + " var builder = document.createElement('div');\n"
617 + " builder.innerHTML = '<input type=\"checkbox\">';\n"
618 + " input = builder.firstChild;\n"
619 + " log(input.value + '-' + input.defaultValue);\n"
620 + " }\n"
621 + "</script>\n"
622 + "</head><body onload='test()'>\n"
623 + "<form>\n"
624 + " <input type='checkbox' id='chkbox1'>\n"
625 + " <input type='checkbox' id='chkbox2' checked='true'>\n"
626 + "</form>\n"
627 + "</body></html>";
628
629 loadPageVerifyTitle2(html);
630 }
631
632
633
634
635 @Test
636 @Alerts({"on-", "on-", "on-", "on-"})
637 public void defaultValuesAfterClone() throws Exception {
638 final String html = DOCTYPE_HTML
639 + "<html><head>\n"
640 + "<script>\n"
641 + LOG_TITLE_FUNCTION
642 + " function test() {\n"
643 + " var input = document.getElementById('chkbox1');\n"
644 + " input = input.cloneNode(false);\n"
645 + " log(input.value + '-' + input.defaultValue);\n"
646
647 + " input = document.getElementById('chkbox2');\n"
648 + " input = input.cloneNode(false);\n"
649 + " log(input.value + '-' + input.defaultValue);\n"
650
651 + " input = document.createElement('input');\n"
652 + " input.type = 'checkbox';\n"
653 + " input = input.cloneNode(false);\n"
654 + " log(input.value + '-' + input.defaultValue);\n"
655
656 + " var builder = document.createElement('div');\n"
657 + " builder.innerHTML = '<input type=\"checkbox\">';\n"
658 + " input = builder.firstChild;\n"
659 + " input = input.cloneNode(false);\n"
660 + " log(input.value + '-' + input.defaultValue);\n"
661 + " }\n"
662 + "</script>\n"
663 + "</head><body onload='test()'>\n"
664 + "<form>\n"
665 + " <input type='checkbox' id='chkbox1'>\n"
666 + " <input type='checkbox' id='chkbox2' checked='true'>\n"
667 + "</form>\n"
668 + "</body></html>";
669
670 loadPageVerifyTitle2(html);
671 }
672
673
674
675
676 @Test
677 @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
678 "newDefault-newDefault", "newDefault-newDefault"})
679 public void resetByClick() throws Exception {
680 final String html = DOCTYPE_HTML
681 + "<html><head>\n"
682 + "<script>\n"
683 + LOG_TITLE_FUNCTION
684 + " function test() {\n"
685 + " var checkbox = document.getElementById('testId');\n"
686 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
687
688 + " document.getElementById('testReset').click;\n"
689 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
690
691 + " checkbox.value = 'newValue';\n"
692 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
693
694 + " document.getElementById('testReset').click;\n"
695 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
696
697 + " checkbox.defaultValue = 'newDefault';\n"
698 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
699
700 + " document.forms[0].reset;\n"
701 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
702 + " }\n"
703 + "</script>\n"
704 + "</head><body onload='test()'>\n"
705 + "<form>\n"
706 + " <input type='checkbox' id='testId' name='radar' value='initial'>\n"
707 + " <input type='reset' id='testReset'>\n"
708 + "</form>\n"
709 + "</body></html>";
710
711 loadPageVerifyTitle2(html);
712 }
713
714
715
716
717 @Test
718 @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
719 "newDefault-newDefault", "newDefault-newDefault"})
720 public void resetByJS() throws Exception {
721 final String html = DOCTYPE_HTML
722 + "<html><head>\n"
723 + "<script>\n"
724 + LOG_TITLE_FUNCTION
725 + " function test() {\n"
726 + " var checkbox = document.getElementById('testId');\n"
727 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
728
729 + " document.forms[0].reset;\n"
730 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
731
732 + " checkbox.value = 'newValue';\n"
733 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
734
735 + " document.forms[0].reset;\n"
736 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
737
738 + " checkbox.defaultValue = 'newDefault';\n"
739 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
740
741 + " document.forms[0].reset;\n"
742 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
743 + " }\n"
744 + "</script>\n"
745 + "</head><body onload='test()'>\n"
746 + "<form>\n"
747 + " <input type='checkbox' id='testId' name='radar' value='initial'>\n"
748 + "</form>\n"
749 + "</body></html>";
750
751 loadPageVerifyTitle2(html);
752 }
753
754
755
756
757 @Test
758 @Alerts({"initial-initial", "default-default", "newValue-newValue", "newDefault-newDefault"})
759 public void defaultValue() throws Exception {
760 final String html = DOCTYPE_HTML
761 + "<html><head>\n"
762 + "<script>\n"
763 + LOG_TITLE_FUNCTION
764 + " function test() {\n"
765 + " var checkbox = document.getElementById('testId');\n"
766 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
767
768 + " checkbox.defaultValue = 'default';\n"
769 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
770
771 + " checkbox.value = 'newValue';\n"
772 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
773 + " checkbox.defaultValue = 'newDefault';\n"
774 + " log(checkbox.value + '-' + checkbox.defaultValue);\n"
775 + " }\n"
776 + "</script>\n"
777 + "</head><body onload='test()'>\n"
778 + "<form>\n"
779 + " <input type='checkbox' id='testId' name='radar' value='initial'>\n"
780 + "</form>\n"
781 + "</body></html>";
782
783 loadPageVerifyTitle2(html);
784 }
785
786
787
788
789
790
791 @Test
792 @Alerts("changed")
793 public void clickShouldTriggerOnchange() throws Exception {
794 final String html = DOCTYPE_HTML
795 + "<html><head>\n"
796 + "<script>\n"
797 + LOG_TITLE_FUNCTION
798 + " function test() {\n"
799 + " var elt = document.getElementById('it');\n"
800 + " elt.click();\n"
801 + " document.getElementById('next').focus();\n"
802 + " }\n"
803 + "</script>\n"
804 + "</head><body onload='test()'>\n"
805 + "<form>\n"
806 + " <input type='checkbox' id='it' onchange='log(\"changed\")'"
807 + " onmousedown='log(\"down\")' onmouseup='log(\"up\")' onfocus='log(\"focused\")'>Check me\n"
808 + " <input type='text' id='next'>\n"
809 + "</form>\n"
810 + "</body></html>";
811 loadPageVerifyTitle2(html);
812 }
813
814
815
816
817 @Test
818 @Alerts({"true", "null", "false", "", "false", "yes"})
819 public void checkedAttribute() throws Exception {
820 final String html = DOCTYPE_HTML
821 + "<html><head>\n"
822 + "<script>\n"
823 + LOG_TITLE_FUNCTION
824 + " function test() {\n"
825 + " var checkbox = document.getElementById('c1');\n"
826 + " log(checkbox.checked);\n"
827 + " log(checkbox.getAttribute('checked'));\n"
828
829 + " checkbox = document.getElementById('c2');\n"
830 + " log(checkbox.checked);\n"
831 + " log(checkbox.getAttribute('checked'));\n"
832
833 + " checkbox = document.getElementById('c3');\n"
834 + " log(checkbox.checked);\n"
835 + " log(checkbox.getAttribute('checked'));\n"
836 + " }\n"
837 + "</script>\n"
838 + "</head><body>\n"
839 + "<form>\n"
840 + " <input type='checkbox' id='c1' name='radar' value='initial'>\n"
841 + " <input type='checkbox' id='c2' name='radar' value='initial' checked>\n"
842 + " <input type='checkbox' id='c3' name='radar' value='initial' checked='yes'>\n"
843 + "</form>\n"
844 + " <button id='clickMe' onClick='test()'>do it</button>\n"
845 + "</body></html>";
846
847 final WebDriver driver = loadPage2(html);
848 driver.findElement(By.id("c1")).click();
849 driver.findElement(By.id("c2")).click();
850 driver.findElement(By.id("c3")).click();
851
852 driver.findElement(By.id("clickMe")).click();
853 verifyTitle2(driver, getExpectedAlerts());
854 }
855
856
857
858
859 @Test
860 @Alerts({"false", "null", "true", "null", "false", "null", "true", "", "false", "", "true", "",
861 "true", "yes", "false", "yes", "true", "yes"})
862 public void checkedAttributeJS() throws Exception {
863 final String html = DOCTYPE_HTML
864 + "<html><head>\n"
865 + "<script>\n"
866 + LOG_TITLE_FUNCTION
867 + " function test() {\n"
868 + " var checkbox = document.getElementById('c1');\n"
869 + " log(checkbox.checked);\n"
870 + " log(checkbox.getAttribute('checked'));\n"
871
872 + " checkbox.checked = true;\n"
873 + " log(checkbox.checked);\n"
874 + " log(checkbox.getAttribute('checked'));\n"
875
876 + " checkbox.checked = false;\n"
877 + " log(checkbox.checked);\n"
878 + " log(checkbox.getAttribute('checked'));\n"
879
880 + " checkbox = document.getElementById('c2');\n"
881 + " log(checkbox.checked);\n"
882 + " log(checkbox.getAttribute('checked'));\n"
883
884 + " checkbox.checked = false;\n"
885 + " log(checkbox.checked);\n"
886 + " log(checkbox.getAttribute('checked'));\n"
887
888 + " checkbox.checked = true;\n"
889 + " log(checkbox.checked);\n"
890 + " log(checkbox.getAttribute('checked'));\n"
891
892 + " checkbox = document.getElementById('c3');\n"
893 + " log(checkbox.checked);\n"
894 + " log(checkbox.getAttribute('checked'));\n"
895
896 + " checkbox.checked = false;\n"
897 + " log(checkbox.checked);\n"
898 + " log(checkbox.getAttribute('checked'));\n"
899
900 + " checkbox.checked = true;\n"
901 + " log(checkbox.checked);\n"
902 + " log(checkbox.getAttribute('checked'));\n"
903 + " }\n"
904 + "</script>\n"
905 + "</head><body onload='test()'>\n"
906 + "<form>\n"
907 + " <input type='checkbox' id='c1' name='radar' value='initial'>\n"
908 + " <input type='checkbox' id='c2' name='radar' value='initial' checked>\n"
909 + " <input type='checkbox' id='c3' name='radar' value='initial' checked='yes'>\n"
910 + "</form>\n"
911 + "</body></html>";
912
913 loadPageVerifyTitle2(html);
914 }
915
916
917
918
919 @Test
920 @Alerts({"false", "null", "false", "null", "true", "", "true", "",
921 "true", "yes", "true", "yes"})
922 public void defaultCheckedAttribute() throws Exception {
923 final String html = DOCTYPE_HTML
924 + "<html><head>\n"
925 + "<script>\n"
926 + " function test() {\n"
927 + LOG_TITLE_FUNCTION
928 + " var checkbox = document.getElementById('c1');\n"
929 + " log(checkbox.defaultChecked);\n"
930 + " log(checkbox.getAttribute('checked'));\n"
931
932 + " checkbox.checked = true;\n"
933 + " log(checkbox.defaultChecked);\n"
934 + " log(checkbox.getAttribute('checked'));\n"
935
936 + " checkbox = document.getElementById('c2');\n"
937 + " log(checkbox.defaultChecked);\n"
938 + " log(checkbox.getAttribute('checked'));\n"
939
940 + " checkbox.checked = false;\n"
941 + " log(checkbox.defaultChecked);\n"
942 + " log(checkbox.getAttribute('checked'));\n"
943
944 + " checkbox = document.getElementById('c3');\n"
945 + " log(checkbox.defaultChecked);\n"
946 + " log(checkbox.getAttribute('checked'));\n"
947
948 + " checkbox.checked = false;\n"
949 + " log(checkbox.defaultChecked);\n"
950 + " log(checkbox.getAttribute('checked'));\n"
951 + " }\n"
952 + "</script>\n"
953 + "</head><body onload='test()'>\n"
954 + "<form>\n"
955 + " <input type='checkbox' id='c1' name='radar' value='initial'>\n"
956 + " <input type='checkbox' id='c2' name='radar' value='initial' checked>\n"
957 + " <input type='checkbox' id='c3' name='radar' value='initial' checked='yes'>\n"
958 + "</form>\n"
959 + "</body></html>";
960
961 loadPageVerifyTitle2(html);
962 }
963
964
965
966
967 @Test
968 @Alerts("--")
969 public void minMaxStep() throws Exception {
970 final String html = DOCTYPE_HTML
971 + "<html>\n"
972 + "<head>\n"
973 + "<script>\n"
974 + LOG_TITLE_FUNCTION
975 + " function test() {\n"
976 + " var input = document.getElementById('tester');\n"
977 + " log(input.min + '-' + input.max + '-' + input.step);\n"
978 + " }\n"
979 + "</script>\n"
980 + "</head>\n"
981 + "<body onload='test()'>\n"
982 + "<form>\n"
983 + " <input type='checkbox' id='tester'>\n"
984 + "</form>\n"
985 + "</body>\n"
986 + "</html>";
987
988 loadPageVerifyTitle2(html);
989 }
990
991
992
993
994 @Test
995 @Alerts({"true", "false", "true", "false", "true"})
996 public void willValidate() throws Exception {
997 final String html = DOCTYPE_HTML
998 + "<html><head>\n"
999 + " <script>\n"
1000 + LOG_TITLE_FUNCTION
1001 + " function test() {\n"
1002 + " log(document.getElementById('o1').willValidate);\n"
1003 + " log(document.getElementById('o2').willValidate);\n"
1004 + " log(document.getElementById('o3').willValidate);\n"
1005 + " log(document.getElementById('o4').willValidate);\n"
1006 + " log(document.getElementById('o5').willValidate);\n"
1007 + " }\n"
1008 + " </script>\n"
1009 + "</head>\n"
1010 + "<body onload='test()'>\n"
1011 + " <form>\n"
1012 + " <input type='checkbox' id='o1'>\n"
1013 + " <input type='checkbox' id='o2' disabled>\n"
1014 + " <input type='checkbox' id='o3' hidden>\n"
1015 + " <input type='checkbox' id='o4' readonly>\n"
1016 + " <input type='checkbox' id='o5' style='display: none'>\n"
1017 + " </form>\n"
1018 + "</body></html>";
1019
1020 loadPageVerifyTitle2(html);
1021 }
1022
1023
1024
1025
1026 @Test
1027 @Alerts({"true",
1028 "false-false-false-false-false-false-false-false-false-true-false",
1029 "true"})
1030 public void validationEmpty() throws Exception {
1031 validation("<input type='checkbox' id='e1'>\n", "");
1032 }
1033
1034
1035
1036
1037 @Test
1038 @Alerts({"false",
1039 "false-true-false-false-false-false-false-false-false-false-false",
1040 "true"})
1041 public void validationCustomValidity() throws Exception {
1042 validation("<input type='checkbox' id='e1'>\n", "elem.setCustomValidity('Invalid');");
1043 }
1044
1045
1046
1047
1048 @Test
1049 @Alerts({"false",
1050 "false-true-false-false-false-false-false-false-false-false-false",
1051 "true"})
1052 public void validationBlankCustomValidity() throws Exception {
1053 validation("<input type='checkbox' id='e1'>\n", "elem.setCustomValidity(' ');\n");
1054 }
1055
1056
1057
1058
1059 @Test
1060 @Alerts({"true",
1061 "false-false-false-false-false-false-false-false-false-true-false",
1062 "true"})
1063 public void validationResetCustomValidity() throws Exception {
1064 validation("<input type='checkbox' id='e1'>\n",
1065 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
1066 }
1067
1068
1069
1070
1071 @Test
1072 @Alerts({"false",
1073 "false-false-false-false-false-false-false-false-false-false-true",
1074 "true"})
1075 public void validationRequired() throws Exception {
1076 validation("<input type='checkbox' id='e1' required>\n", "");
1077 }
1078
1079
1080
1081
1082 @Test
1083 @Alerts({"true",
1084 "false-false-false-false-false-false-false-false-false-true-false",
1085 "true"})
1086 public void validationRequiredChecked() throws Exception {
1087 validation("<input type='checkbox' id='e1' required checked>\n", "");
1088 }
1089
1090
1091
1092
1093 @Test
1094 @Alerts({"true",
1095 "false-false-false-false-false-false-false-false-false-true-false",
1096 "true"})
1097 public void validationRequiredClicked() throws Exception {
1098 validation("<input type='checkbox' id='e1' required>\n", "elem.click();");
1099 }
1100
1101
1102
1103
1104 @Test
1105 @Alerts({"false",
1106 "false-false-false-false-false-false-false-false-false-false-true",
1107 "true"})
1108 public void validationRequiredClickUncheck() throws Exception {
1109 validation("<input type='checkbox' id='e1' required checked>\n", "elem.click();");
1110 }
1111
1112 private void validation(final String htmlPart, final String jsPart) throws Exception {
1113 final String html = DOCTYPE_HTML
1114 + "<html><head>\n"
1115 + " <script>\n"
1116 + LOG_TITLE_FUNCTION
1117 + " function logValidityState(s) {\n"
1118 + " log(s.badInput"
1119 + "+ '-' + s.customError"
1120 + "+ '-' + s.patternMismatch"
1121 + "+ '-' + s.rangeOverflow"
1122 + "+ '-' + s.rangeUnderflow"
1123 + "+ '-' + s.stepMismatch"
1124 + "+ '-' + s.tooLong"
1125 + "+ '-' + s.tooShort"
1126 + " + '-' + s.typeMismatch"
1127 + " + '-' + s.valid"
1128 + " + '-' + s.valueMissing);\n"
1129 + " }\n"
1130 + " function test() {\n"
1131 + " var elem = document.getElementById('e1');\n"
1132 + jsPart
1133 + " log(elem.checkValidity());\n"
1134 + " logValidityState(elem.validity);\n"
1135 + " log(elem.willValidate);\n"
1136 + " }\n"
1137 + " </script>\n"
1138 + "</head>\n"
1139 + "<body onload='test()'>\n"
1140 + " <form>\n"
1141 + htmlPart
1142 + " </form>\n"
1143 + "</body></html>";
1144
1145 loadPageVerifyTitle2(html);
1146 }
1147 }