1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.htmlunit.CollectingAlertHandler;
21 import org.htmlunit.MockWebConnection;
22 import org.htmlunit.SimpleWebTestCase;
23 import org.htmlunit.WebClient;
24 import org.htmlunit.junit.annotation.Alerts;
25 import org.htmlunit.junit.annotation.BuggyWebDriver;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32
33
34
35
36
37
38 public class ClickableElementTest extends SimpleWebTestCase {
39
40
41
42
43
44
45
46
47
48 private void onClickPageTest(final String htmlContent) throws Exception {
49 setExpectedAlerts("foo");
50 onClickPageTest(htmlContent, 1);
51 }
52
53
54
55
56
57
58
59
60
61
62 private void onClickPageTest(final String htmlContent, final int numClicks) throws Exception {
63 onClickPageTest(htmlContent, numClicks, false);
64 }
65
66
67
68
69
70
71
72
73
74
75 private void onClickPageTest(final String htmlContent, final int numClicks,
76 final boolean exceptionOnError) throws Exception {
77
78 final WebClient client = getWebClientWithMockWebConnection();
79
80 final MockWebConnection webConnection = getMockWebConnection();
81 webConnection.setDefaultResponse(htmlContent);
82 client.getOptions().setThrowExceptionOnScriptError(exceptionOnError);
83
84 final List<String> collectedAlerts = new ArrayList<>();
85 final CollectingAlertHandler alertHandler = new CollectingAlertHandler(collectedAlerts);
86 client.setAlertHandler(alertHandler);
87
88 final HtmlPage page = client.getPage(URL_FIRST);
89 final HtmlElement clickable = page.getHtmlElementById("clickId");
90
91 for (int i = 0; i < numClicks; i++) {
92 clickable.click();
93 }
94
95 assertEquals(getExpectedAlerts(), collectedAlerts);
96 }
97
98
99
100
101
102
103
104 private void onClickBodyTest(final String htmlBody) throws Exception {
105 onClickPageTest(DOCTYPE_HTML + "<html><head><title>foo</title></head>\n" + htmlBody
106 + "</html>");
107 }
108
109
110
111
112
113
114
115 private void onClickSimpleTest(final String tagName) throws Exception {
116 onClickBodyTest("<body><" + tagName + " id='clickId' onClick='alert(\"foo\")'>Text</" + tagName + "></body>\n");
117 }
118
119
120
121
122
123
124 @Test
125 public void anchor_onClick() throws Exception {
126 onClickSimpleTest("a");
127 }
128
129
130
131
132
133
134 @Test
135 public void abbreviation_onClick() throws Exception {
136 onClickSimpleTest("abbr");
137 }
138
139
140
141
142
143
144 @Test
145 public void acronym_onClick() throws Exception {
146 onClickSimpleTest("acronym");
147 }
148
149
150
151
152
153
154 @Test
155 public void address_onClick() throws Exception {
156 onClickSimpleTest("address");
157 }
158
159
160
161
162
163
164 @Test
165 public void bold_onClick() throws Exception {
166 onClickSimpleTest("b");
167 }
168
169
170
171
172
173
174 @Test
175 public void big_onClick() throws Exception {
176 onClickSimpleTest("big");
177 }
178
179
180
181
182
183
184 @Test
185 public void blockquote_onClick() throws Exception {
186 onClickSimpleTest("blockquote");
187 }
188
189
190
191
192
193
194 @Test
195 public void body_onClick() throws Exception {
196 onClickBodyTest("<body id='clickId' onClick='alert(\"foo\")'>Text</body>\n");
197 }
198
199
200
201
202
203
204 @Test
205 public void button_onClick() throws Exception {
206 onClickBodyTest("<body><form><button id='clickId' onClick='alert(\"foo\")'>Item</button></form></body>\n");
207 }
208
209
210
211
212
213
214 @Test
215 @Alerts({"foo0", "foo1"})
216 public void button_onClickTwice() throws Exception {
217 onClickPageTest("<body><form>\n"
218 + "<button id='clickId' onClick='alert(\"foo\" + count++); return false;'>Item</button>\n"
219 + "<script> var count = 0 </script>\n"
220 + "</form></body>\n", 2);
221 }
222
223
224
225
226
227
228 @Test
229 public void cite_onClick() throws Exception {
230 onClickSimpleTest("cite");
231 }
232
233
234
235
236
237
238 @Test
239 public void code_onClick() throws Exception {
240 onClickSimpleTest("code");
241 }
242
243
244
245
246
247
248 @Test
249 public void tableColumn_onClick() throws Exception {
250 onClickBodyTest("<body><table><caption>Caption</caption><colgroup>\n"
251 + "<col id='clickId' onClick='alert(\"foo\")'/></colgroup><thead><tr><th>\n"
252 + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>\n"
253 + "th>Header</th></tr></tfoot></table></body>\n");
254 }
255
256
257
258
259
260
261 @Test
262 public void tableColumnGroup_onClick() throws Exception {
263 onClickBodyTest("<body><table><caption>Caption</caption>\n"
264 + "<colgroup id='clickId' onClick='alert(\"foo\")'><col/></colgroup><thead>\n"
265 + "<tr><th>Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot>\n"
266 + "<tr><th>Header</th></tr></tfoot></table></body>\n");
267 }
268
269
270
271
272
273
274 @Test
275 public void center_onClick() throws Exception {
276 onClickSimpleTest("center");
277 }
278
279
280
281
282
283
284 @Test
285 public void tableCaption_onClick() throws Exception {
286 onClickBodyTest("<body><table><caption id='clickId' onClick='alert(\"foo\")'>\n"
287 + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>\n"
288 + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>\n"
289 + "</tfoot></table></body>\n");
290 }
291
292
293
294
295
296
297 @Test
298 public void definitionDescription_onClick() throws Exception {
299 onClickBodyTest("<body><dl><dt>Term</dt><dd id='clickId' onClick='alert(\"foo\")'>Definition</dd></dl></body>");
300 }
301
302
303
304
305
306
307
308 @Test
309 public void javaScriptError_onClick() throws Exception {
310 onClickPageTest(DOCTYPE_HTML
311 + "<html><head></head><body>\n"
312 + "<form method='POST'><input type='button' id='clickId' onclick='y()'></form>\n"
313 + "</body></html>",
314 1, false);
315 }
316
317
318
319
320
321
322 @Test
323 public void definition_onClick() throws Exception {
324 onClickSimpleTest("dfn");
325 }
326
327
328
329
330
331
332 @Test
333 public void directory_onClick() throws Exception {
334 onClickSimpleTest("dir");
335 }
336
337
338
339
340
341
342 @Test
343 public void definitionList_onClick() throws Exception {
344 onClickBodyTest("<body><dl id='clickId' onClick='alert(\"foo\")'><dt>Term</dt><dd>Definition</dd></dl></body>");
345 }
346
347
348
349
350
351
352 @Test
353 public void definitionTerm_onClick() throws Exception {
354 onClickBodyTest("<body><dl><dt id='clickId' onClick='alert(\"foo\")'>Term</dt><dd>Definition</dd></dl></body>");
355 }
356
357
358
359
360
361
362 @Test
363 public void deletedText_onClick() throws Exception {
364 onClickSimpleTest("del");
365 }
366
367
368
369
370
371
372 @Test
373 public void division_onClick() throws Exception {
374 onClickSimpleTest("div");
375 }
376
377
378
379
380
381
382 @Test
383 public void emphasis_onClick() throws Exception {
384 onClickSimpleTest("em");
385 }
386
387
388
389
390
391
392 @Test
393 public void fieldSet_onClick() throws Exception {
394 onClickBodyTest("<body><form><fieldset id='clickId' onClick='alert(\"foo\")'>\n"
395 + "<legend>Legend</legend></fieldset></form></body>\n");
396 }
397
398
399
400
401
402
403 @Test
404 public void form_onClick() throws Exception {
405 onClickSimpleTest("form");
406 }
407
408
409
410
411
412
413 @Test
414 public void italics_onClick() throws Exception {
415 onClickSimpleTest("i");
416 }
417
418
419
420
421
422
423 @Test
424 public void header1_onClick() throws Exception {
425 onClickSimpleTest("h1");
426 }
427
428
429
430
431
432
433 @Test
434 public void header2_onClick() throws Exception {
435 onClickSimpleTest("h2");
436 }
437
438
439
440
441
442
443 @Test
444 public void header3_onClick() throws Exception {
445 onClickSimpleTest("h3");
446 }
447
448
449
450
451
452
453 @Test
454 public void header4_onClick() throws Exception {
455 onClickSimpleTest("h4");
456 }
457
458
459
460
461
462
463 @Test
464 public void header5_onClick() throws Exception {
465 onClickSimpleTest("h5");
466 }
467
468
469
470
471
472
473 @Test
474 public void header6_onClick() throws Exception {
475 onClickSimpleTest("h6");
476 }
477
478
479
480
481
482
483 @Test
484 public void horizontalRule_onClick() throws Exception {
485 onClickSimpleTest("hr");
486 }
487
488
489
490
491
492
493 @Test
494 public void input_onClick() throws Exception {
495 onClickBodyTest("<body><form><input id='clickId' onClick='alert(\"foo\")'>Item</input></form></body>\n");
496 }
497
498
499
500
501
502
503 @Test
504 public void insertedText_onClick() throws Exception {
505 onClickSimpleTest("ins");
506 }
507
508
509
510
511
512
513 @Test
514 public void keyboard_onClick() throws Exception {
515 onClickSimpleTest("kbd");
516 }
517
518
519
520
521
522
523 @Test
524 public void label_onClick() throws Exception {
525 onClickBodyTest("<body><form><label id='clickId' onClick='alert(\"foo\")'>Item</label></form></body>\n");
526 }
527
528
529
530
531
532
533 @Test
534 public void legend_onClick() throws Exception {
535 onClickBodyTest("<body><form><fieldset><legend id='clickId' onClick='alert(\"foo\")'>\n"
536 + "Legend</legend></fieldset></form></body>\n");
537 }
538
539
540
541
542
543
544 @Test
545 public void listItem_onClick() throws Exception {
546 onClickBodyTest("<body><ol><li id='clickId' onClick='alert(\"foo\")'>Item</li></ol></body>\n");
547 }
548
549
550
551
552
553
554 @Test
555 public void menu_onClick() throws Exception {
556 onClickBodyTest("<body><menu id='clickId' onClick='alert(\"foo\")'><li>Item</li></menu></body>\n");
557 }
558
559
560
561
562
563
564 @Test
565 public void object_onClick() throws Exception {
566 onClickSimpleTest("object");
567 }
568
569
570
571
572
573
574 @Test
575 @Alerts("foo")
576 @BuggyWebDriver(CHROME = "")
577
578 public void option_onClick() throws Exception {
579 final String htmlContent = DOCTYPE_HTML
580 + "<html><head><title>foo</title></head>\n"
581 + "<body><form><select size='2'><option id='clickId' onClick='alert(\"foo\")'>\n"
582 + "Option</option></select></form></body>\n"
583 + "</html>";
584
585 onClickPageTest(htmlContent, 1);
586 }
587
588
589
590
591
592
593 @Test
594 public void optionGroup_onClick() throws Exception {
595 onClickBodyTest("<body><form><select><optgroup id='clickId' onClick='alert(\"foo\")'>\n"
596 + "<option>Option</option></optgroup></select></form></body>\n");
597 }
598
599
600
601
602
603
604 @Test
605 public void orderedList_onClick() throws Exception {
606 onClickBodyTest("<body><ol id='clickId' onClick='alert(\"foo\")'><li>Item</li></ol></body>\n");
607 }
608
609
610
611
612
613
614 @Test
615 public void paragraph_onClick() throws Exception {
616 onClickSimpleTest("p");
617 }
618
619
620
621
622
623
624 @Test
625 public void pre_onClick() throws Exception {
626 onClickSimpleTest("pre");
627 }
628
629
630
631
632
633
634 @Test
635 public void quotation_onClick() throws Exception {
636 onClickSimpleTest("q");
637 }
638
639
640
641
642
643
644 @Test
645 public void strikethrough_onClick() throws Exception {
646 onClickSimpleTest("s");
647 }
648
649
650
651
652
653
654 @Test
655 public void sample_onClick() throws Exception {
656 onClickSimpleTest("samp");
657 }
658
659
660
661
662
663
664 @Test
665 public void select_onClick() throws Exception {
666 onClickBodyTest("<body><form><select id='clickId' onClick='alert(\"foo\")'>\n"
667 + "<option>Option</option></select></form></body>\n");
668 }
669
670
671
672
673
674
675 @Test
676 public void small_onClick() throws Exception {
677 onClickSimpleTest("small");
678 }
679
680
681
682
683
684
685 @Test
686 public void span_onClick() throws Exception {
687 onClickSimpleTest("span");
688 }
689
690
691
692
693
694
695 @Test
696 public void strike_onClick() throws Exception {
697 onClickSimpleTest("strike");
698 }
699
700
701
702
703
704
705 @Test
706 public void subscript_onClick() throws Exception {
707 onClickSimpleTest("sub");
708 }
709
710
711
712
713
714
715 @Test
716 public void superscript_onClick() throws Exception {
717 onClickSimpleTest("sup");
718 }
719
720
721
722
723
724
725 @Test
726 public void table_onClick() throws Exception {
727 onClickBodyTest("<body><table id='clickId' onClick='alert(\"foo\")'><caption>\n"
728 + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>\n"
729 + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>\n"
730 + "</tfoot></table></body>\n");
731 }
732
733
734
735
736
737
738 @Test
739 public void tableBody_onClick() throws Exception {
740 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
741 + "</colgroup><thead><tr><th>Header</th></tr></thead>\n"
742 + "<tbody id='clickId' onClick='alert(\"foo\")'><tr><td>Data</td></tr>\n"
743 + "</tbody><tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
744 }
745
746
747
748
749
750
751 @Test
752 public void tableDataCell_onClick() throws Exception {
753 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
754 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr>\n"
755 + "<td id='clickId' onClick='alert(\"foo\")'>Data</td></tr></tbody>\n"
756 + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
757 }
758
759
760
761
762
763
764 @Test
765 public void textarea_onClick() throws Exception {
766 onClickBodyTest("<body><form><textarea id='clickId' onClick='alert(\"foo\")'>Item</textarea></form></body>\n");
767 }
768
769
770
771
772
773
774 @Test
775 public void tableFooter_onClick() throws Exception {
776 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
777 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr><td>Data</td>\n"
778 + "</tr></tbody><tfoot id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>\n"
779 + "</tr></tfoot></table></body>\n");
780 }
781
782
783
784
785
786
787 @Test
788 public void tableHeaderCell_onClick() throws Exception {
789 onClickBodyTest("<body><table><caption>Caption</caption><colgroup>\n"
790 + "<col/></colgroup><thead><tr><th id='clickId' onClick='alert(\"foo\")'>\n"
791 + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>\n"
792 + "<th>Header</th></tr></tfoot></table></body>\n");
793 }
794
795
796
797
798
799
800 @Test
801 public void tableHeader_onClick() throws Exception {
802 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
803 + "</colgroup><thead id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>\n"
804 + "</tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th>\n"
805 + "</tr></tfoot></table></body>\n");
806 }
807
808
809
810
811
812
813 @Test
814 public void tableRow_onClick() throws Exception {
815 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
816 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody>\n"
817 + "<tr id='clickId' onClick='alert(\"foo\")'><td>Data</td></tr></tbody>\n"
818 + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
819 }
820
821
822
823
824
825
826 @Test
827 public void tableRow_onClickSetOnLoad() throws Exception {
828 onClickPageTest(DOCTYPE_HTML
829 + "<html><head>\n"
830 + "<script language='JavaScript'>\n"
831 + "function doFoo() { alert('foo'); }\n"
832 + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}\n"
833 + "</script>\n"
834 + "</head><body onload=\"doOnload();\">\n"
835 + "<table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>\n"
836 + "</body></html>");
837 }
838
839
840
841
842 @Test
843 public void checkbox_onClickUpdatesStateFirst() throws Exception {
844 onClickPageTest(DOCTYPE_HTML
845 + "<html><head>\n"
846 + "<script language='JavaScript'>\n"
847 + "function doFoo(event) { if (this.checked) alert('foo'); else alert('bar'); }\n"
848 + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}\n"
849 + "</script>\n"
850 + "</head><body onload=\"doOnload();\">\n"
851 + "<input type='checkbox' id='clickId'>\n"
852 + "</body></html>");
853 }
854
855
856
857
858
859
860 @Test
861 public void tableRow_onClickSetByNestedScript() throws Exception {
862 onClickBodyTest("<body><table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>\n"
863 + "<script language='JavaScript'>\n"
864 + "function doFoo(event) { alert('foo'); }\n"
865 + "document.getElementById('clickId').onclick = doFoo;</script></body>\n");
866 }
867
868
869
870
871
872
873 @Test
874 public void teletype_onClick() throws Exception {
875 onClickSimpleTest("tt");
876 }
877
878
879
880
881
882
883 @Test
884 public void underline_onClick() throws Exception {
885 onClickSimpleTest("u");
886 }
887
888
889
890
891
892
893 @Test
894 public void unorderedList_onClick() throws Exception {
895 onClickBodyTest("<body><ul id='clickId' onClick='alert(\"foo\")'><li>Item</li></ul></body>\n");
896 }
897
898
899
900
901
902
903 @Test
904 public void variable_onClick() throws Exception {
905 onClickSimpleTest("var");
906 }
907
908
909
910
911
912
913 @Test
914 @Alerts("foo")
915 public void setOnClick() throws Exception {
916 onClickPageTest(DOCTYPE_HTML
917 + "<html><body><form>\n"
918 + "<button type='button' id='clickId' onclick='alert(\"foo\"); onclick=null;'>Item</button>\n"
919 + "</form></body></html>", 2);
920 }
921 }