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.List;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link HtmlOption}.
25   *
26   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Daniel Gredler
30   * @author Ronald Brill
31   */
32  public class HtmlOptionTest extends SimpleWebTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      public void select() throws Exception {
39          final String htmlContent = DOCTYPE_HTML
40              + "<html>\n"
41              + "<head><title>foo</title></head>\n"
42              + "<body>\n"
43              + "  <form id='form1'>\n"
44              + "    <select name='select1' id='select1'>\n"
45              + "      <option value='option1' id='option1'>Option1</option>\n"
46              + "      <option value='option2' id='option2' selected='selected'>Option2</option>\n"
47              + "      <option value='option3' id='option3'>Option3</option>\n"
48              + "    </select>\n"
49              + "  </form>\n"
50              + "</body></html>";
51          final HtmlPage page = loadPage(htmlContent);
52  
53          final HtmlOption option1 = page.getHtmlElementById("option1");
54          final HtmlOption option2 = page.getHtmlElementById("option2");
55          final HtmlOption option3 = page.getHtmlElementById("option3");
56  
57          assertFalse(option1.isSelected());
58          assertTrue(option2.isSelected());
59          assertFalse(option3.isSelected());
60  
61          option3.setSelected(true);
62  
63          assertFalse(option1.isSelected());
64          assertFalse(option2.isSelected());
65          assertTrue(option3.isSelected());
66  
67          option3.setSelected(false);
68  
69          assertFalse(option1.isSelected());
70          assertFalse(option2.isSelected());
71          assertFalse(option3.isSelected());
72      }
73  
74      /**
75       * @throws Exception if the test fails
76       */
77      @Test
78      public void getValue() throws Exception {
79          final String htmlContent = DOCTYPE_HTML
80              + "<html>\n"
81              + "<head><title>foo</title></head>\n"
82              + "<body>\n"
83              + "  <form id='form1'>\n"
84              + "    <select name='select1' id='select1'>\n"
85              + "      <option value='option1' id='option1'>Option1</option>\n"
86              + "      <option id='option2' selected>Number Two</option>\n"
87              + "  </select>\n"
88              + "  </form>\n"
89              + "</body></html>";
90  
91          final HtmlPage page = loadPage(htmlContent);
92  
93          final HtmlOption option1 = page.getHtmlElementById("option1");
94          final HtmlOption option2 = page.getHtmlElementById("option2");
95  
96          assertEquals("option1", option1.getValueAttribute());
97          assertEquals("Number Two", option2.getValueAttribute());
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     public void getValue_ContentsIsValue() throws Exception {
105         final String htmlContent = DOCTYPE_HTML
106             + "<html>\n"
107             + "<head><title>foo</title></head>\n"
108             + "<body>\n"
109             + "  <form id='form1'>\n"
110             + "    <select name='select1' id='select1'>\n"
111             + "      <option id='option1'>Option1</option>\n"
112             + "      <option id='option2' selected>Number Two</option>\n"
113             + "      <option id='option3'>\n  Number 3 with blanks </option>\n"
114             + "    </select>\n"
115             + "  </form>\n"
116             + "</body></html>";
117 
118         final HtmlPage page = loadPage(htmlContent);
119 
120         final HtmlOption option1 = page.getHtmlElementById("option1");
121         assertEquals("Option1", option1.getValueAttribute());
122 
123         final HtmlOption option2 = page.getHtmlElementById("option2");
124         assertEquals("Number Two", option2.getValueAttribute());
125 
126         final HtmlOption option3 = page.getHtmlElementById("option3");
127         assertEquals("Number 3 with blanks", option3.getValueAttribute());
128     }
129 
130     /**
131      * @throws Exception if the test fails
132      */
133     @Test
134     public void click() throws Exception {
135         final String htmlContent = DOCTYPE_HTML
136             + "<html><body>\n"
137             + "  <form id='form1'>\n"
138             + "    <select name='select1' id='select1'>\n"
139             + "      <option id='option1'>Option1</option>\n"
140             + "      <option id='option2' selected>Number Two</option>\n"
141             + "    </select>\n"
142             + "</form></body></html>";
143         final HtmlPage page = loadPage(htmlContent);
144 
145         final HtmlOption option1 = page.getHtmlElementById("option1");
146         assertFalse(option1.isSelected());
147         option1.click();
148         assertTrue(option1.isSelected());
149         option1.click();
150         assertTrue(option1.isSelected());
151     }
152 
153     /**
154      * @throws Exception if the test fails
155      */
156     @Test
157     public void asNormalizedText() throws Exception {
158         final String htmlContent = DOCTYPE_HTML
159             + "<html>\n"
160             + "<head><title>foo</title></head>\n"
161             + "<body>\n"
162             + "  <form>\n"
163             + "    <select>\n"
164             + "      <option id='option1'>option1</option>\n"
165             + "      <option id='option2' label='Number Two'/>\n"
166             + "      <option id='option3' label='overridden'>Number Three</option>\n"
167             + "      <option id='option4'>Number&nbsp;4</option>\n"
168             + "    </select>\n"
169             + "  </form>\n"
170             + "</body></html>";
171 
172         final HtmlPage page = loadPage(htmlContent);
173 
174         final HtmlOption option1 = page.getHtmlElementById("option1");
175         final HtmlOption option2 = page.getHtmlElementById("option2");
176         final HtmlOption option3 = page.getHtmlElementById("option3");
177         final HtmlOption option4 = page.getHtmlElementById("option4");
178 
179         assertEquals("option1", option1.asNormalizedText());
180         assertEquals("", option2.asNormalizedText());
181         assertEquals("Number Three", option3.asNormalizedText());
182         assertEquals("Number 4", option4.asNormalizedText());
183     }
184 
185     /**
186      * @throws Exception if an error occurs
187      */
188     @Test
189     @Alerts({"false", "false", "true", "true", "false"})
190     public void disabled() throws Exception {
191         final String html = DOCTYPE_HTML
192             + "<html><body onload='test()'><form name='f'>\n"
193             + "  <select name='s' id='s'>\n"
194             + "    <option value='o1' id='o1'>One</option>\n"
195             + "    <option value='o2' id='o2' disabled='disabled'>Two</option>\n"
196             + "  </select>\n"
197             + "  <script>\n"
198             + "    function test() {\n"
199             + "      var s = document.getElementById('s');\n"
200             + "      var o1 = document.getElementById('o1');\n"
201             + "      var o2 = document.getElementById('o2');\n"
202             + "      alert(s.disabled);\n"
203             + "      alert(o1.disabled);\n"
204             + "      alert(o2.disabled);\n"
205             + "      o1.disabled = true;\n"
206             + "      o2.disabled = false;\n"
207             + "      alert(o1.disabled);\n"
208             + "      alert(o2.disabled);\n"
209             + "    }\n"
210             + "  </script>\n"
211             + "</form></body></html>";
212 
213         final HtmlPage page = loadPageWithAlerts(html);
214         assertTrue(((HtmlOption) page.getElementById("o1")).isDisabled());
215         assertFalse(((HtmlOption) page.getElementById("o2")).isDisabled());
216     }
217 
218     /**
219      * Test case for #1864.
220      *
221      * @throws Exception if an error occurs
222      */
223     @Test
224     public void isSelected() throws Exception {
225         final String html = DOCTYPE_HTML
226                 + "<html><body>"
227                 + "  <select multiple><option value='a'>a</option><option value='b'>b</option></select>\n"
228                 + "</body></html>";
229 
230         final HtmlPage page = loadPage(html);
231         final HtmlSelect select = (HtmlSelect) page.getElementsByTagName("select").get(0);
232         assertTrue(select.isMultipleSelectEnabled());
233 
234         final List<HtmlOption> options = select.getOptions();
235         for (final HtmlOption option : options) {
236             option.setSelected(true);
237         }
238 
239         for (final HtmlOption option : options) {
240             assertTrue(option.isSelected());
241         }
242     }
243 }