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