-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathHtmlInputTest.java
More file actions
291 lines (249 loc) · 10.7 KB
/
HtmlInputTest.java
File metadata and controls
291 lines (249 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.html;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.htmlunit.HttpMethod;
import org.htmlunit.MockWebConnection;
import org.htmlunit.SimpleWebTestCase;
import org.htmlunit.WebClient;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.junit.jupiter.api.Test;
/**
* Tests for {@link HtmlInput}.
*
* @author Mike Bowler
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Anton Demydenko
* @author Ronny Shapiro
* @author Ronald Brill
*/
public final class HtmlInputTest extends SimpleWebTestCase {
/**
* Tests that selecting one radio button will deselect all the others.
* @exception Exception If the test fails
*/
@Test
public void radioButtonsAreMutuallyExclusive() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='radio' name='foo' value='1' selected='selected'/>\n"
+ "<input type='radio' name='foo' value='2'/>\n"
+ "<input type='radio' name='foo' value='3'/>\n"
+ "<input type='submit' name='button' value='foo'/>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlRadioButtonInput radioButton = form.getFirstByXPath(
"//input[@type='radio' and @name='foo' and @value='2']");
final HtmlSubmitInput pushButton = form.getInputByName("button");
radioButton.setChecked(true);
// Test that only one value for the radio button is being passed back to the server
final HtmlPage secondPage = pushButton.click();
assertEquals("url", URL_FIRST + "?foo=2&button=foo", secondPage.getUrl());
assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
assertNotNull(secondPage);
}
/**
* @throws Exception if the test fails
*/
@Test
public void setChecked_CheckBox() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='checkbox' name='foo'/>\n"
+ "<input type='checkbox' name='bar'/>\n"
+ "<input type='submit' name='button' value='foo'/>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlCheckBoxInput checkbox = form.getInputByName("foo");
assertFalse("Initial state", checkbox.isChecked());
checkbox.setChecked(true);
assertTrue("After setSelected(true)", checkbox.isChecked());
checkbox.setChecked(false);
assertFalse("After setSelected(false)", checkbox.isChecked());
}
/**
* @throws Exception if the test fails
*/
@Test
public void getChecked_RadioButton() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='radio' name='radio1'>\n"
+ "<input type='RADIO' name='radio1' value='bar' checked>\n"
+ "<input type='submit' name='button' value='foo'>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final List<HtmlRadioButtonInput> radioButtons = form.getRadioButtonsByName("radio1");
assertEquals(2, radioButtons.size());
assertFalse(radioButtons.get(0).isChecked());
assertTrue(radioButtons.get(1).isChecked());
}
/**
* @throws Exception if the test fails
*/
@Test
public void setValueAttribute() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='text' name='text1' onchange='alert(\"changed\")')>\n"
+ "</form></body></html>";
final List<String> collectedAlerts = new ArrayList<>();
final HtmlPage page = loadPage(htmlContent, collectedAlerts);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlTextInput input = form.getInputByName("text1");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
input.setValueAttribute("foo");
input.setValue("foo");
assertNotEquals(Arrays.asList("changed").toString(), collectedAlerts.toString());
}
/**
* @throws Exception if the test fails
*/
@Test
public void checkboxDefaultValue() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='checkbox' name='checkbox1')>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlCheckBoxInput input = form.getInputByName("checkbox1");
assertEquals("", input.getValueAttribute());
assertEquals("on", input.getValue());
}
/**
* Tests that clicking a radio button will select it.
* @exception Exception If the test fails
*/
@Test
public void clickRadioButton() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='radio' name='foo' value='1' selected='selected'/>\n"
+ "<input type='radio' name='foo' value='2'/>\n"
+ "<input type='radio' name='foo' value='3'/>\n"
+ "<input type='submit' name='button' value='foo'/>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlRadioButtonInput radioButton = form.getFirstByXPath(
"//input[@type='radio' and @name='foo' and @value='2']");
assertFalse("Should not be checked before click", radioButton.isChecked());
radioButton.click();
assertTrue("Should be checked after click", radioButton.isChecked());
}
/**
* Tests that default type of input is text.
* @exception Exception If the test fails
*/
@Test
public void inputNoType() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input name='foo'/>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = page.getHtmlElementById("form1");
assertEquals("text", form.getInputByName("foo").getTypeAttribute());
}
/**
* @throws Exception if the test fails
*/
@Test
public void onChangeHandlerNotFiredOnLoad() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='file' name='text1' onchange='alert(\"changed\")')>\n"
+ "</form></body></html>";
final List<String> collectedAlerts = new ArrayList<>();
loadPage(htmlContent, collectedAlerts);
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
/**
* @throws Exception
* if the test fails
*/
@Test
public void testRequiredValidation() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html>\n"
+ "<head></head>\n"
+ "<body>\n"
+ "<form id='form1'>\n"
+ " <input id='foo' required='required'>\n"
+ "</form>\n"
+ "</body></html>";
final HtmlPage page = loadPage(htmlContent);
final HtmlInput input = (HtmlInput) page.getElementById("foo");
assertFalse(input.isValid());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void changeType_javascriptEngineDisabled() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='text' name='text1' onchange='alert(\"changed\")')>\n"
+ "</form></body></html>";
try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
final HtmlPage page = loadPage(webClient, htmlContent, null, URL_FIRST);
assertFalse(page.getWebClient().isJavaScriptEngineEnabled());
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlTextInput input = form.getInputByName("text1");
input.setAttribute("type", "hidden");
}
}
/**
* @throws Exception if an error occurs
*/
@Test
public void changeType_javascriptDisabled() throws Exception {
final String htmlContent = DOCTYPE_HTML
+ "<html><head><title>foo</title></head><body>\n"
+ "<form id='form1'>\n"
+ "<input type='text' name='text1' onchange='alert(\"changed\")')>\n"
+ "</form></body></html>";
try (WebClient webClient = new WebClient(getBrowserVersion())) {
webClient.getOptions().setJavaScriptEnabled(false);
final HtmlPage page = loadPage(webClient, htmlContent, null, URL_FIRST);
final HtmlForm form = page.getHtmlElementById("form1");
final HtmlTextInput input = form.getInputByName("text1");
final ScriptableObject jsPeer = input.getScriptableObject();
assertNotNull(jsPeer);
input.setAttribute("type", "hidden");
assertNotNull(input.getScriptableObject());
assertNotEquals(jsPeer, input.getScriptableObject());
}
}
}