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 HTMLFormControlsCollection}.
23   *
24   * @author Lai Quang Duong
25   * @author Ronald Brill
26   */
27  public class HTMLFormControlsCollectionTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception on test failure
31       */
32      @Test
33      @Alerts({"true", "true", "2", "true", "1", "null"})
34      public void namedItem() throws Exception {
35          final String html = DOCTYPE_HTML
36                  + "<html><head>\n"
37                  + "<script>\n"
38                  + LOG_TITLE_FUNCTION
39                  + "  function test() {\n"
40                  + "    var elements = document.form.elements;\n"
41                  + "\n"
42                  + "    log(elements instanceof HTMLFormControlsCollection);\n"
43                  + "    log(elements.namedItem('first') instanceof RadioNodeList);\n"
44                  + "    log(elements.namedItem('first').value);\n"
45                  + "\n"
46                  + "    log(elements.namedItem('second') instanceof HTMLInputElement);\n"
47                  + "    log(elements.namedItem('second').value);\n"
48                  + "\n"
49                  + "    log(elements.namedItem('third'));\n"
50                  + "  }\n"
51                  + "</script>\n"
52                  + "</head><body onload='test()'>\n"
53                  + "<form name='form'>\n"
54                  + "<input type='text' name='first' value='0'/>\n"
55                  + "<input type='radio' name='first'/>\n"
56                  + "<input type='radio' name='first' value='2' checked/>\n"
57                  + "<input type='radio' name='first' value='3'/>\n"
58                  + "\n"
59                  + "<input type='radio' name='second' value='1'/>\n"
60                  + "</form>"
61                  + "</body></html>\n";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception on test failure
68       */
69      @Test
70      @Alerts({"first", "first", "first", "first", "second"})
71      public void iterable() throws Exception {
72          final String html = DOCTYPE_HTML
73                  + "<html><head>\n"
74                  + "<script>\n"
75                  + LOG_TITLE_FUNCTION
76                  + "  function test() {\n"
77                  + "    for (let elem of document.form.elements) {\n"
78                  + "      log(elem.name)\n"
79                  + "    }\n"
80                  + "  }\n"
81                  + "</script>\n"
82                  + "</head><body onload='test()'>\n"
83                  + "<form name='form'>\n"
84                  + "<input type='text' name='first' value='0'/>\n"
85                  + "<input type='radio' name='first'/>\n"
86                  + "<input type='radio' name='first' value='2' checked/>\n"
87                  + "<input type='radio' name='first' value='3'/>\n"
88                  + "\n"
89                  + "<input type='radio' name='second' value='1'/>\n"
90                  + "</form>"
91                  + "</body></html>\n";
92  
93          loadPageVerifyTitle2(html);
94      }
95  }