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.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class HTMLFormControlsCollectionTest extends WebDriverTestCase {
31
32
33
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
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 }