1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.List;
18
19 import org.htmlunit.SimpleWebTestCase;
20 import org.htmlunit.junit.BrowserRunner;
21 import org.htmlunit.junit.annotation.Alerts;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24
25
26
27
28
29
30
31
32
33
34 @RunWith(BrowserRunner.class)
35 public class HtmlOptionTest extends SimpleWebTestCase {
36
37
38
39
40 @Test
41 public void select() throws Exception {
42 final String htmlContent = DOCTYPE_HTML
43 + "<html>\n"
44 + "<head><title>foo</title></head>\n"
45 + "<body>\n"
46 + " <form id='form1'>\n"
47 + " <select name='select1' id='select1'>\n"
48 + " <option value='option1' id='option1'>Option1</option>\n"
49 + " <option value='option2' id='option2' selected='selected'>Option2</option>\n"
50 + " <option value='option3' id='option3'>Option3</option>\n"
51 + " </select>\n"
52 + " </form>\n"
53 + "</body></html>";
54 final HtmlPage page = loadPage(htmlContent);
55
56 final HtmlOption option1 = page.getHtmlElementById("option1");
57 final HtmlOption option2 = page.getHtmlElementById("option2");
58 final HtmlOption option3 = page.getHtmlElementById("option3");
59
60 assertFalse(option1.isSelected());
61 assertTrue(option2.isSelected());
62 assertFalse(option3.isSelected());
63
64 option3.setSelected(true);
65
66 assertFalse(option1.isSelected());
67 assertFalse(option2.isSelected());
68 assertTrue(option3.isSelected());
69
70 option3.setSelected(false);
71
72 assertFalse(option1.isSelected());
73 assertFalse(option2.isSelected());
74 assertFalse(option3.isSelected());
75 }
76
77
78
79
80 @Test
81 public void getValue() throws Exception {
82 final String htmlContent = DOCTYPE_HTML
83 + "<html>\n"
84 + "<head><title>foo</title></head>\n"
85 + "<body>\n"
86 + " <form id='form1'>\n"
87 + " <select name='select1' id='select1'>\n"
88 + " <option value='option1' id='option1'>Option1</option>\n"
89 + " <option id='option2' selected>Number Two</option>\n"
90 + " </select>\n"
91 + " </form>\n"
92 + "</body></html>";
93
94 final HtmlPage page = loadPage(htmlContent);
95
96 final HtmlOption option1 = page.getHtmlElementById("option1");
97 final HtmlOption option2 = page.getHtmlElementById("option2");
98
99 assertEquals("option1", option1.getValueAttribute());
100 assertEquals("Number Two", option2.getValueAttribute());
101 }
102
103
104
105
106 @Test
107 public void getValue_ContentsIsValue() throws Exception {
108 final String htmlContent = DOCTYPE_HTML
109 + "<html>\n"
110 + "<head><title>foo</title></head>\n"
111 + "<body>\n"
112 + " <form id='form1'>\n"
113 + " <select name='select1' id='select1'>\n"
114 + " <option id='option1'>Option1</option>\n"
115 + " <option id='option2' selected>Number Two</option>\n"
116 + " <option id='option3'>\n Number 3 with blanks </option>\n"
117 + " </select>\n"
118 + " </form>\n"
119 + "</body></html>";
120
121 final HtmlPage page = loadPage(htmlContent);
122
123 final HtmlOption option1 = page.getHtmlElementById("option1");
124 assertEquals("Option1", option1.getValueAttribute());
125
126 final HtmlOption option2 = page.getHtmlElementById("option2");
127 assertEquals("Number Two", option2.getValueAttribute());
128
129 final HtmlOption option3 = page.getHtmlElementById("option3");
130 assertEquals("Number 3 with blanks", option3.getValueAttribute());
131 }
132
133
134
135
136 @Test
137 public void click() throws Exception {
138 final String htmlContent = DOCTYPE_HTML
139 + "<html><body>\n"
140 + " <form id='form1'>\n"
141 + " <select name='select1' id='select1'>\n"
142 + " <option id='option1'>Option1</option>\n"
143 + " <option id='option2' selected>Number Two</option>\n"
144 + " </select>\n"
145 + "</form></body></html>";
146 final HtmlPage page = loadPage(htmlContent);
147
148 final HtmlOption option1 = page.getHtmlElementById("option1");
149 assertFalse(option1.isSelected());
150 option1.click();
151 assertTrue(option1.isSelected());
152 option1.click();
153 assertTrue(option1.isSelected());
154 }
155
156
157
158
159 @Test
160 public void asNormalizedText() throws Exception {
161 final String htmlContent = DOCTYPE_HTML
162 + "<html>\n"
163 + "<head><title>foo</title></head>\n"
164 + "<body>\n"
165 + " <form>\n"
166 + " <select>\n"
167 + " <option id='option1'>option1</option>\n"
168 + " <option id='option2' label='Number Two'/>\n"
169 + " <option id='option3' label='overridden'>Number Three</option>\n"
170 + " <option id='option4'>Number 4</option>\n"
171 + " </select>\n"
172 + " </form>\n"
173 + "</body></html>";
174
175 final HtmlPage page = loadPage(htmlContent);
176
177 final HtmlOption option1 = page.getHtmlElementById("option1");
178 final HtmlOption option2 = page.getHtmlElementById("option2");
179 final HtmlOption option3 = page.getHtmlElementById("option3");
180 final HtmlOption option4 = page.getHtmlElementById("option4");
181
182 assertEquals("option1", option1.asNormalizedText());
183 assertEquals("", option2.asNormalizedText());
184 assertEquals("Number Three", option3.asNormalizedText());
185 assertEquals("Number 4", option4.asNormalizedText());
186 }
187
188
189
190
191 @Test
192 @Alerts({"false", "false", "true", "true", "false"})
193 public void disabled() throws Exception {
194 final String html = DOCTYPE_HTML
195 + "<html><body onload='test()'><form name='f'>\n"
196 + " <select name='s' id='s'>\n"
197 + " <option value='o1' id='o1'>One</option>\n"
198 + " <option value='o2' id='o2' disabled='disabled'>Two</option>\n"
199 + " </select>\n"
200 + " <script>\n"
201 + " function test() {\n"
202 + " var s = document.getElementById('s');\n"
203 + " var o1 = document.getElementById('o1');\n"
204 + " var o2 = document.getElementById('o2');\n"
205 + " alert(s.disabled);\n"
206 + " alert(o1.disabled);\n"
207 + " alert(o2.disabled);\n"
208 + " o1.disabled = true;\n"
209 + " o2.disabled = false;\n"
210 + " alert(o1.disabled);\n"
211 + " alert(o2.disabled);\n"
212 + " }\n"
213 + " </script>\n"
214 + "</form></body></html>";
215
216 final HtmlPage page = loadPageWithAlerts(html);
217 assertTrue(((HtmlOption) page.getElementById("o1")).isDisabled());
218 assertFalse(((HtmlOption) page.getElementById("o2")).isDisabled());
219 }
220
221
222
223
224
225
226 @Test
227 public void isSelected() throws Exception {
228 final String html = DOCTYPE_HTML
229 + "<html><body>"
230 + " <select multiple><option value='a'>a</option><option value='b'>b</option></select>\n"
231 + "</body></html>";
232
233 final HtmlPage page = loadPage(html);
234 final HtmlSelect select = (HtmlSelect) page.getElementsByTagName("select").get(0);
235 assertTrue(select.isMultipleSelectEnabled());
236
237 final List<HtmlOption> options = select.getOptions();
238 for (final HtmlOption option : options) {
239 option.setSelected(true);
240 }
241
242 for (final HtmlOption option : options) {
243 assertTrue(option.isSelected());
244 }
245 }
246 }