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