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.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HTMLOutputElement}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class HTMLOutputElementTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if an error occurs
31       */
32      @Test
33      @Alerts({"0", "2", "1", "2", "1", "1"})
34      public void labels() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><head>\n"
37              + "  <script>\n"
38              + LOG_TITLE_FUNCTION
39              + "    function test() {\n"
40              + "      debug(document.getElementById('e1'));\n"
41              + "      debug(document.getElementById('e2'));\n"
42              + "      debug(document.getElementById('e3'));\n"
43              + "      debug(document.getElementById('e4'));\n"
44              + "      var labels = document.getElementById('e4').labels;\n"
45              + "      document.body.removeChild(document.getElementById('l4'));\n"
46              + "      debug(document.getElementById('e4'));\n"
47              + "      log(labels ? labels.length : labels);\n"
48              + "    }\n"
49              + "    function debug(e) {\n"
50              + "      log(e.labels ? e.labels.length : e.labels);\n"
51              + "    }\n"
52              + "  </script>\n"
53              + "</head>\n"
54              + "<body onload='test()'>\n"
55              + "  <output id='e1'>e 1</output><br>\n"
56              + "  <label>something <label> click here <output id='e2'>e 2</output></label></label><br>\n"
57              + "  <label for='e3'> and here</label>\n"
58              + "  <output id='e3'>e 3</output><br>\n"
59              + "  <label id='l4' for='e4'> what about</label>\n"
60              + "  <label> this<output id='e4'>e 4</output></label><br>\n"
61              + "</body></html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if an error occurs
68       */
69      @Test
70      @Alerts({"false", "false", "false", "false", "false"})
71      public void willValidate() throws Exception {
72          final String html = DOCTYPE_HTML
73                  + "<html><head>\n"
74                  + "  <script>\n"
75                  + LOG_TITLE_FUNCTION
76                  + "    function test() {\n"
77                  + "      log(document.getElementById('i1').willValidate);\n"
78                  + "      log(document.getElementById('i2').willValidate);\n"
79                  + "      log(document.getElementById('i3').willValidate);\n"
80                  + "      log(document.getElementById('i4').willValidate);\n"
81                  + "      log(document.getElementById('i5').willValidate);\n"
82                  + "    }\n"
83                  + "  </script>\n"
84                  + "</head>\n"
85                  + "<body onload='test()'>\n"
86                  + "  <form>\n"
87                  + "    <output id='i1'>button</output>"
88                  + "    <output id='i2' disabled></output>"
89                  + "    <output id='i3' hidden></output>"
90                  + "    <output id='i4' readonly></output>"
91                  + "    <output id='i5' style='display: none'></output>"
92                  + "  </form>\n"
93                  + "</body></html>";
94  
95          loadPageVerifyTitle2(html);
96      }
97  }