1
2
3
4
5
6
7
8
9
10
11
12
13
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
23
24
25
26
27 public class HTMLFormControlsCollectionTest extends WebDriverTestCase {
28
29
30
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
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 }