View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for various clickable elements.
30   *
31   * @author David K. Taylor
32   * @author Chris Erskine
33   * @author Marc Guillemot
34   * @author Ahmed Ashour
35   * @author Frank Danek
36   * @author Ronald Brill
37   */
38  public class ClickableElementTest extends SimpleWebTestCase {
39  
40      /**
41       * Full page driver for onClick tests.
42       *
43       * @param htmlContent HTML fragment for body of page with clickable element
44       *        identified by clickId ID attribute. Must have onClick that raises
45       *        an alert of "foo".
46       * @throws Exception if the test fails
47       */
48      private void onClickPageTest(final String htmlContent) throws Exception {
49          setExpectedAlerts("foo");
50          onClickPageTest(htmlContent, 1);
51      }
52  
53      /**
54       * Full page driver for onClick tests.
55       *
56       * @param htmlContent HTML fragment for body of page with clickable element
57       *        identified by clickId ID attribute.
58       * @param numClicks number of times to click element
59       * @param expectedAlerts array of expected popup values
60       * @throws Exception if the test fails
61       */
62      private void onClickPageTest(final String htmlContent, final int numClicks) throws Exception {
63          onClickPageTest(htmlContent, numClicks, false);
64      }
65  
66      /**
67       * Full page driver for onClick tests.
68       *
69       * @param htmlContent HTML fragment for body of page with clickable element identified by clickId ID attribute
70       * @param numClicks number of times to click element
71       * @param expectedAlerts array of expected popup values
72       * @param exceptionOnError indicate to throw on error
73       * @throws Exception if the test fails
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       * Body driver for onClick tests.
100      *
101      * @param htmlBody HTML text body
102      * @throws Exception if the test fails
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      * Simple tag name driver for onClick tests.
111      *
112      * @param tagName HTML tag name for simple tag with text body
113      * @throws Exception if the test fails
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      * Test onClick handler and click method of anchor element.
121      *
122      * @throws Exception if the test fails
123      */
124     @Test
125     public void anchor_onClick() throws Exception {
126         onClickSimpleTest("a");
127     }
128 
129     /**
130      * Test onClick handler and click method of abbreviation element.
131      *
132      * @throws Exception if the test fails
133      */
134     @Test
135     public void abbreviation_onClick() throws Exception {
136         onClickSimpleTest("abbr");
137     }
138 
139     /**
140      * Test onClick handler and click method of acronym element.
141      *
142      * @throws Exception if the test fails
143      */
144     @Test
145     public void acronym_onClick() throws Exception {
146         onClickSimpleTest("acronym");
147     }
148 
149     /**
150      * Test onClick handler and click method of address element.
151      *
152      * @throws Exception if the test fails
153      */
154     @Test
155     public void address_onClick() throws Exception {
156         onClickSimpleTest("address");
157     }
158 
159     /**
160      * Test onClick handler and click method of bold element.
161      *
162      * @throws Exception if the test fails
163      */
164     @Test
165     public void bold_onClick() throws Exception {
166         onClickSimpleTest("b");
167     }
168 
169     /**
170      * Test onClick handler and click method of big element.
171      *
172      * @throws Exception if the test fails
173      */
174     @Test
175     public void big_onClick() throws Exception {
176         onClickSimpleTest("big");
177     }
178 
179     /**
180      * Test onClick handler and click method of blockquote element.
181      *
182      * @throws Exception if the test fails
183      */
184     @Test
185     public void blockquote_onClick() throws Exception {
186         onClickSimpleTest("blockquote");
187     }
188 
189     /**
190      * Test onClick handler and click method of body element.
191      *
192      * @throws Exception if the test fails
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      * Test onClick handler and click method of button element.
201      *
202      * @throws Exception if the test fails
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      * Test onClick handler can be called multiple times.
211      *
212      * @throws Exception if the test fails
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      * Test onClick handler and click method of cite element.
225      *
226      * @throws Exception if the test fails
227      */
228     @Test
229     public void cite_onClick() throws Exception {
230         onClickSimpleTest("cite");
231     }
232 
233     /**
234      * Test onClick handler and click method of code element.
235      *
236      * @throws Exception if the test fails
237      */
238     @Test
239     public void code_onClick() throws Exception {
240         onClickSimpleTest("code");
241     }
242 
243     /**
244      * Test onClick handler and click method of table column element.
245      *
246      * @throws Exception if the test fails
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      * Test onClick handler and click method of table column group element.
258      *
259      * @throws Exception if the test fails
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      * Test onClick handler and click method of center element.
271      *
272      * @throws Exception if the test fails
273      */
274     @Test
275     public void center_onClick() throws Exception {
276         onClickSimpleTest("center");
277     }
278 
279     /**
280      * Test onClick handler and click method of table caption element.
281      *
282      * @throws Exception if the test fails
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      * Test onClick handler and click method of definition description dd element.
294      *
295      * @throws Exception if the test fails
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      * Test that no NPE is thrown when JS fails on link click
304      * and WebClient.setThrowExceptionOnScriptError(false) is used.
305      * Test for bug #329.
306      * @throws Exception if the test fails
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      * Test onClick handler and click method of definition element.
319      *
320      * @throws Exception if the test fails
321      */
322     @Test
323     public void definition_onClick() throws Exception {
324         onClickSimpleTest("dfn");
325     }
326 
327     /**
328      * Test onClick handler and click method of directory element.
329      *
330      * @throws Exception if the test fails
331      */
332     @Test
333     public void directory_onClick() throws Exception {
334         onClickSimpleTest("dir");
335     }
336 
337     /**
338      * Test onClick handler and click method of definition list dl element.
339      *
340      * @throws Exception if the test fails
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      * Test onClick handler and click method of definition term dt element.
349      *
350      * @throws Exception if the test fails
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      * Test onClick handler and click method of deleted text element.
359      *
360      * @throws Exception if the test fails
361      */
362     @Test
363     public void deletedText_onClick() throws Exception {
364         onClickSimpleTest("del");
365     }
366 
367     /**
368      * Test onClick handler and click method of division element.
369      *
370      * @throws Exception if the test fails
371      */
372     @Test
373     public void division_onClick() throws Exception {
374         onClickSimpleTest("div");
375     }
376 
377     /**
378      * Test onClick handler and click method of emphasis element.
379      *
380      * @throws Exception if the test fails
381      */
382     @Test
383     public void emphasis_onClick() throws Exception {
384         onClickSimpleTest("em");
385     }
386 
387     /**
388      * Test onClick handler and click method of field set element.
389      *
390      * @throws Exception if the test fails
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      * Test onClick handler and click method of form element.
400      *
401      * @throws Exception if the test fails
402      */
403     @Test
404     public void form_onClick() throws Exception {
405         onClickSimpleTest("form");
406     }
407 
408     /**
409      * Test onClick handler and click method of italics element.
410      *
411      * @throws Exception if the test fails
412      */
413     @Test
414     public void italics_onClick() throws Exception {
415         onClickSimpleTest("i");
416     }
417 
418     /**
419      * Test onClick handler and click method of header1 element.
420      *
421      * @throws Exception if the test fails
422      */
423     @Test
424     public void header1_onClick() throws Exception {
425         onClickSimpleTest("h1");
426     }
427 
428     /**
429      * Test onClick handler and click method of header2 element.
430      *
431      * @throws Exception if the test fails
432      */
433     @Test
434     public void header2_onClick() throws Exception {
435         onClickSimpleTest("h2");
436     }
437 
438     /**
439      * Test onClick handler and click method of header3 element.
440      *
441      * @throws Exception if the test fails
442      */
443     @Test
444     public void header3_onClick() throws Exception {
445         onClickSimpleTest("h3");
446     }
447 
448     /**
449      * Test onClick handler and click method of header4 element.
450      *
451      * @throws Exception if the test fails
452      */
453     @Test
454     public void header4_onClick() throws Exception {
455         onClickSimpleTest("h4");
456     }
457 
458     /**
459      * Test onClick handler and click method of header5 element.
460      *
461      * @throws Exception if the test fails
462      */
463     @Test
464     public void header5_onClick() throws Exception {
465         onClickSimpleTest("h5");
466     }
467 
468     /**
469      * Test onClick handler and click method of header6 element.
470      *
471      * @throws Exception if the test fails
472      */
473     @Test
474     public void header6_onClick() throws Exception {
475         onClickSimpleTest("h6");
476     }
477 
478     /**
479      * Test onClick handler and click method of horizontal rule element.
480      *
481      * @throws Exception if the test fails
482      */
483     @Test
484     public void horizontalRule_onClick() throws Exception {
485         onClickSimpleTest("hr");
486     }
487 
488     /**
489      * Test onClick handler and click method of input element.
490      *
491      * @throws Exception if the test fails
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      * Test onClick handler and click method of inserted text element.
500      *
501      * @throws Exception if the test fails
502      */
503     @Test
504     public void insertedText_onClick() throws Exception {
505         onClickSimpleTest("ins");
506     }
507 
508     /**
509      * Test onClick handler and click method of keyboard element.
510      *
511      * @throws Exception if the test fails
512      */
513     @Test
514     public void keyboard_onClick() throws Exception {
515         onClickSimpleTest("kbd");
516     }
517 
518     /**
519      * Test onClick handler and click method of label element.
520      *
521      * @throws Exception if the test fails
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      * Test onClick handler and click method of legend element.
530      *
531      * @throws Exception if the test fails
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      * Test onClick handler and click method of List Item element.
541      *
542      * @throws Exception if the test fails
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      * Test onClick handler and click method of menu element.
551      *
552      * @throws Exception if the test fails
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      * Test onClick handler and click method of object element.
561      *
562      * @throws Exception if the test fails
563      */
564     @Test
565     public void object_onClick() throws Exception {
566         onClickSimpleTest("object");
567     }
568 
569     /**
570      * Test onClick handler and click method of option element.
571      *
572      * @throws Exception if the test fails
573      */
574     @Test
575     @Alerts("foo")
576     @BuggyWebDriver(CHROME = "")
577     // ChromeDriver does not generate a "foo" but it occurs manually
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      * Test onClick handler and click method of Option Group element.
590      *
591      * @throws Exception if the test fails
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      * Test onClick handler and click method of Ordered List element.
601      *
602      * @throws Exception if the test fails
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      * Test onClick handler and click method of paragraph element.
611      *
612      * @throws Exception if the test fails
613      */
614     @Test
615     public void paragraph_onClick() throws Exception {
616         onClickSimpleTest("p");
617     }
618 
619     /**
620      * Test onClick handler and click method of pre element.
621      *
622      * @throws Exception if the test fails
623      */
624     @Test
625     public void pre_onClick() throws Exception {
626         onClickSimpleTest("pre");
627     }
628 
629     /**
630      * Test onClick handler and click method of inline Quotation element.
631      *
632      * @throws Exception if the test fails
633      */
634     @Test
635     public void quotation_onClick() throws Exception {
636         onClickSimpleTest("q");
637     }
638 
639     /**
640      * Test onClick handler and click method of strikethrough element.
641      *
642      * @throws Exception if the test fails
643      */
644     @Test
645     public void strikethrough_onClick() throws Exception {
646         onClickSimpleTest("s");
647     }
648 
649     /**
650      * Test onClick handler and click method of sample element.
651      *
652      * @throws Exception if the test fails
653      */
654     @Test
655     public void sample_onClick() throws Exception {
656         onClickSimpleTest("samp");
657     }
658 
659     /**
660      * Test onClick handler and click method of select element.
661      *
662      * @throws Exception if the test fails
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      * Test onClick handler and click method of small element.
672      *
673      * @throws Exception if the test fails
674      */
675     @Test
676     public void small_onClick() throws Exception {
677         onClickSimpleTest("small");
678     }
679 
680     /**
681      * Test onClick handler and click method of span element.
682      *
683      * @throws Exception if the test fails
684      */
685     @Test
686     public void span_onClick() throws Exception {
687         onClickSimpleTest("span");
688     }
689 
690     /**
691      * Test onClick handler and click method of strike element.
692      *
693      * @throws Exception if the test fails
694      */
695     @Test
696     public void strike_onClick() throws Exception {
697         onClickSimpleTest("strike");
698     }
699 
700     /**
701      * Test onClick handler and click method of subscript element.
702      *
703      * @throws Exception if the test fails
704      */
705     @Test
706     public void subscript_onClick() throws Exception {
707         onClickSimpleTest("sub");
708     }
709 
710     /**
711      * Test onClick handler and click method of superscript element.
712      *
713      * @throws Exception if the test fails
714      */
715     @Test
716     public void superscript_onClick() throws Exception {
717         onClickSimpleTest("sup");
718     }
719 
720     /**
721      * Test onClick handler and click method of table element.
722      *
723      * @throws Exception if the test fails
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      * Test onClick handler and click method of table body element.
735      *
736      * @throws Exception if the test fails
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      * Test onClick handler and click method of table data cell element.
748      *
749      * @throws Exception if the test fails
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      * Test onClick handler and click method of textarea element.
761      *
762      * @throws Exception if the test fails
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      * Test onClick handler and click method of table footer element.
771      *
772      * @throws Exception if the test fails
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      * Test onClick handler and click method of table header cell element.
784      *
785      * @throws Exception if the test fails
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      * Test onClick handler and click method of table header element.
797      *
798      * @throws Exception if the test fails
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      * Test onClick handler and click method of table row element.
810      *
811      * @throws Exception if the test fails
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      * Test when HtmlTableRow.onclick is set by JavaScript.
823      *
824      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * Test when HtmlTableRow.onclick is set by a JavaScript.
857      *
858      * @throws Exception if the test fails
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      * Test onClick handler and click method of teletype element.
870      *
871      * @throws Exception if the test fails
872      */
873     @Test
874     public void teletype_onClick() throws Exception {
875         onClickSimpleTest("tt");
876     }
877 
878     /**
879      * Test onClick handler and click method of underline element.
880      *
881      * @throws Exception if the test fails
882      */
883     @Test
884     public void underline_onClick() throws Exception {
885         onClickSimpleTest("u");
886     }
887 
888     /**
889      * Test onClick handler and click method of unordered List element.
890      *
891      * @throws Exception if the test fails
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      * Test onClick handler and click method of variable element.
900      *
901      * @throws Exception if the test fails
902      */
903     @Test
904     public void variable_onClick() throws Exception {
905         onClickSimpleTest("var");
906     }
907 
908     /**
909      * Test setting onClick handler from inside the onClick handler.
910      *
911      * @throws Exception if the test fails
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 }