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.SimpleWebTestCase;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  
25  /**
26   * Tests for {@link HtmlLabel}.
27   *
28   * @author Marc Guillemot
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   * @author Frank Danek
32   */
33  @RunWith(BrowserRunner.class)
34  public class HtmlLabel2Test extends SimpleWebTestCase {
35  
36      /**
37       * Verifies that a checkbox is toggled when the related label is clicked.
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void test_click() throws Exception {
42          final String htmlContent = DOCTYPE_HTML
43              + "<html><head><title>foo</title></head><body>\n"
44              + "<form id='form1'>\n"
45              + "  <input type='checkbox' name='checkbox' id='testCheckbox' onclick='alert(\"checkbox\")'/>\n"
46              + "  <label for='testCheckbox' id='testLabel' onclick='alert(\"label\")'>Check me</label>\n"
47              + "</form></body></html>";
48          final List<String> collectedAlerts = new ArrayList<>();
49          final HtmlPage page = loadPage(htmlContent, collectedAlerts);
50          final HtmlCheckBoxInput checkBox = page.getHtmlElementById("testCheckbox");
51  
52          assertFalse(checkBox.isChecked());
53          final HtmlLabel label = page.getHtmlElementById("testLabel");
54          label.click();
55          assertTrue(checkBox.isChecked());
56          final String[] expectedAlerts = {"label", "checkbox"};
57          assertEquals(expectedAlerts, collectedAlerts);
58          label.click();
59          assertFalse(checkBox.isChecked());
60      }
61  
62      /**
63       * @throws Exception if an error occurs
64       */
65      @Test
66      public void triggerRadio() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html>\n"
69              + "<body>\n"
70              + "  <ul>\n"
71              + "  <li>\n"
72              + "    <label id='radio1Label' for='radio1'>Radio 1</label>\n"
73              + "    <input id='radio1' name='radios' value='1' type='radio'>\n"
74              + "  </li>\n"
75              + "</ul>\n"
76              + "</body></html>";
77  
78          getWebClient().getOptions().setJavaScriptEnabled(false);
79          final HtmlPage page = loadPage(html);
80          assertFalse(((HtmlRadioButtonInput) page.getElementById("radio1")).isChecked());
81  
82          page.getElementById("radio1Label").click();
83  
84          assertTrue(((HtmlRadioButtonInput) page.getElementById("radio1")).isChecked());
85      }
86  
87      /**
88       * @throws Exception if an error occurs
89       */
90      @Test
91      public void triggerRadioComplexCase() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html>\n"
94              + "<body>\n"
95              + "  <ul>\n"
96              + "  <li>\n"
97              + "    <label id='radio1Label' for='radio1'>\n"
98              + "      <span>\n"
99              + "        <input id='radio1' name='radios' value='1' type='radio'>\n"
100             + "        <span id='radio1Span'>Radio 1</span>\n"
101             + "      </span>\n"
102             + "    </label>\n"
103             + "  </li>\n"
104             + "</ul>\n"
105             + "</body></html>";
106 
107         getWebClient().getOptions().setJavaScriptEnabled(false);
108         final HtmlPage page = loadPage(html);
109         assertFalse(((HtmlRadioButtonInput) page.getElementById("radio1")).isChecked());
110 
111         page.getElementById("radio1Label").click();
112 
113         assertTrue(((HtmlRadioButtonInput) page.getElementById("radio1")).isChecked());
114     }
115 
116     /**
117      * @throws Exception if an error occurs
118      */
119     @Test
120     public void triggerCheckbox() throws Exception {
121         final String html = DOCTYPE_HTML
122             + "<html>\n"
123             + "<body>\n"
124             + "  <ul>\n"
125             + "  <li>\n"
126             + "    <label id='check1Label' for='check1'>Checkbox 1</label>\n"
127             + "    <input id='check1' name='checks' value='1' type='checkbox'>\n"
128             + "  </li>\n"
129             + "</ul>\n"
130             + "</body></html>";
131 
132         getWebClient().getOptions().setJavaScriptEnabled(false);
133         final HtmlPage page = loadPage(html);
134         assertFalse(((HtmlCheckBoxInput) page.getElementById("check1")).isChecked());
135 
136         page.getElementById("check1Label").click();
137 
138         assertTrue(((HtmlCheckBoxInput) page.getElementById("check1")).isChecked());
139     }
140 
141     /**
142      * @throws Exception if an error occurs
143      */
144     @Test
145     public void triggerCheckboxComplexCase() throws Exception {
146         final String html = DOCTYPE_HTML
147             + "<html>\n"
148             + "<body>\n"
149             + "  <ul>\n"
150             + "  <li>\n"
151             + "    <label id='check1Label' for='check1'>\n"
152             + "      <span>\n"
153             + "        <input id='check1' name='checks' value='1' type='checkbox'>\n"
154             + "        <span id='check1Span'>Checkbox 1</span>\n"
155             + "      </span>\n"
156             + "    </label>\n"
157             + "  </li>\n"
158             + "</ul>\n"
159             + "</body></html>";
160 
161         getWebClient().getOptions().setJavaScriptEnabled(false);
162         final HtmlPage page = loadPage(html);
163         assertFalse(((HtmlCheckBoxInput) page.getElementById("check1")).isChecked());
164 
165         page.getElementById("check1Label").click();
166 
167         assertTrue(((HtmlCheckBoxInput) page.getElementById("check1")).isChecked());
168     }
169 }