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.javascript.host.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.htmlunit.html.HtmlPage;
19  import org.htmlunit.html.HtmlSelect;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link HTMLOptionElement}.
25   *
26   * @author Marc Guillemot
27   * @author Ahmed Ashour
28   * @author Frank Danek
29   * @author Ronald Brill
30   */
31  public class HTMLOptionElementTest extends SimpleWebTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"value1", "text1", "label1", "value2", "text2", "text2"})
38      public void label() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head><title>foo</title><script>\n"
41              + "function test() {\n"
42              + "  var s = document.getElementById('testSelect');\n"
43              + "  var lastIndex = s.length;\n"
44              + "  s.length += 1;\n"
45              + "  s[lastIndex].value = 'value2';\n"
46              + "  s[lastIndex].text  = 'text2';\n"
47              + "  alert(s[0].value);\n"
48              + "  alert(s[0].text);\n"
49              + "  alert(s[0].label);\n"
50              + "  alert(s[1].value);\n"
51              + "  alert(s[1].text);\n"
52              + "  alert(s[1].label);\n"
53              + "}\n"
54              + "</script></head><body onload='test()'>\n"
55              + "  <select id='testSelect'>\n"
56              + "    <option value='value1' label='label1'>text1</option>\n"
57              + "  </select>\n"
58              + "</form>\n"
59              + "</body></html>";
60  
61          final HtmlPage page = loadPageWithAlerts(html);
62          final HtmlSelect select = page.getHtmlElementById("testSelect");
63          assertEquals("value1", select.getOption(0).getValueAttribute());
64          assertEquals("text1", select.getOption(0).getTextContent());
65          assertEquals("label1", select.getOption(0).getLabelAttribute());
66          assertEquals("value2", select.getOption(1).getValueAttribute());
67          assertEquals("text2", select.getOption(1).getTextContent());
68          assertEquals("text2", select.getOption(1).getLabelAttribute());
69      }
70  }