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