Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/FormHandlingTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
23
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
24
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
25
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
26
import static org.openqa.selenium.testing.drivers.Browser.IE;
27
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
28
29
import java.io.File;
30
import java.io.IOException;
31
import org.junit.jupiter.api.Test;
32
import org.openqa.selenium.environment.webserver.Page;
33
import org.openqa.selenium.testing.Ignore;
34
import org.openqa.selenium.testing.JupiterTestBase;
35
import org.openqa.selenium.testing.NotYetImplemented;
36
37
class FormHandlingTest extends JupiterTestBase {
38
39
@Test
40
void testShouldClickOnSubmitInputElements() {
41
driver.get(pages.formPage);
42
driver.findElement(By.id("submitButton")).click();
43
wait.until(titleIs("We Arrive Here"));
44
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
45
}
46
47
@Test
48
void testClickingOnUnclickableElementsDoesNothing() {
49
driver.get(pages.formPage);
50
driver.findElement(By.xpath("//body")).click();
51
}
52
53
@Test
54
void testShouldBeAbleToClickImageButtons() {
55
driver.get(pages.formPage);
56
driver.findElement(By.id("imageButton")).click();
57
wait.until(titleIs("We Arrive Here"));
58
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
59
}
60
61
@Test
62
void testShouldBeAbleToSubmitForms() {
63
driver.get(pages.formPage);
64
driver.findElement(By.name("login")).submit();
65
wait.until(titleIs("We Arrive Here"));
66
}
67
68
@Test
69
void testShouldSubmitAFormWhenAnyInputElementWithinThatFormIsSubmitted() {
70
driver.get(pages.formPage);
71
driver.findElement(By.id("checky")).submit();
72
wait.until(titleIs("We Arrive Here"));
73
}
74
75
@Test
76
void testShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted() {
77
driver.get(pages.formPage);
78
driver.findElement(By.xpath("//form/p")).submit();
79
wait.until(titleIs("We Arrive Here"));
80
}
81
82
@Test
83
void testShouldNotBeAbleToSubmitAFormThatDoesNotExist() {
84
driver.get(pages.formPage);
85
WebElement element = driver.findElement(By.name("SearchableText"));
86
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(element::submit);
87
}
88
89
@Test
90
void testShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue() {
91
driver.get(pages.javascriptPage);
92
WebElement textarea = driver.findElement(By.id("keyUpArea"));
93
String cheesy = "brie and cheddar";
94
textarea.sendKeys(cheesy);
95
assertThat(textarea.getAttribute("value")).isEqualTo(cheesy);
96
}
97
98
@Test
99
void testSendKeysKeepsCapitalization() {
100
driver.get(pages.javascriptPage);
101
WebElement textarea = driver.findElement(By.id("keyUpArea"));
102
String cheesey = "BrIe And CheDdar";
103
textarea.sendKeys(cheesey);
104
assertThat(textarea.getAttribute("value")).isEqualTo(cheesey);
105
}
106
107
@Test
108
@NotYetImplemented(FIREFOX)
109
@NotYetImplemented(SAFARI)
110
public void testShouldSubmitAFormUsingTheNewlineLiteral() {
111
driver.get(pages.formPage);
112
WebElement nestedForm = driver.findElement(By.id("nested_form"));
113
WebElement input = nestedForm.findElement(By.name("x"));
114
input.sendKeys("\n");
115
wait.until(titleIs("We Arrive Here"));
116
assertThat(driver.getCurrentUrl()).endsWith("?x=name");
117
}
118
119
@Test
120
void testShouldSubmitAFormUsingTheEnterKey() {
121
driver.get(pages.formPage);
122
WebElement nestedForm = driver.findElement(By.id("nested_form"));
123
WebElement input = nestedForm.findElement(By.name("x"));
124
input.sendKeys(Keys.ENTER);
125
wait.until(titleIs("We Arrive Here"));
126
assertThat(driver.getCurrentUrl()).endsWith("?x=name");
127
}
128
129
@Test
130
void testShouldEnterDataIntoFormFields() {
131
driver.get(pages.xhtmlTestPage);
132
WebElement element =
133
driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));
134
String originalValue = element.getAttribute("value");
135
assertThat(originalValue).isEqualTo("change");
136
137
element.clear();
138
element.sendKeys("some text");
139
140
element = driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));
141
String newFormValue = element.getAttribute("value");
142
assertThat(newFormValue).isEqualTo("some text");
143
}
144
145
@Test
146
void testShouldBeAbleToAlterTheContentsOfAFileUploadInputElement() throws IOException {
147
driver.get(pages.formPage);
148
WebElement uploadElement = driver.findElement(By.id("upload"));
149
assertThat(uploadElement.getAttribute("value")).isEmpty();
150
151
File file = File.createTempFile("test", "txt");
152
file.deleteOnExit();
153
154
uploadElement.sendKeys(file.getAbsolutePath());
155
156
String uploadPath = uploadElement.getAttribute("value");
157
assertThat(uploadPath.endsWith(file.getName())).isTrue();
158
}
159
160
@Test
161
void testShouldBeAbleToSendKeysToAFileUploadInputElementInAnXhtmlDocument() throws IOException {
162
driver.get(pages.xhtmlFormPage);
163
WebElement uploadElement = driver.findElement(By.id("file"));
164
assertThat(uploadElement.getAttribute("value")).isEmpty();
165
166
File file = File.createTempFile("test", "txt");
167
file.deleteOnExit();
168
169
uploadElement.sendKeys(file.getAbsolutePath());
170
171
String uploadPath = uploadElement.getAttribute("value");
172
assertThat(uploadPath.endsWith(file.getName())).isTrue();
173
}
174
175
@Test
176
@Ignore(value = SAFARI, reason = "Hanging")
177
public void testShouldBeAbleToUploadTheSameFileTwice() throws IOException {
178
File file = File.createTempFile("test", "txt");
179
file.deleteOnExit();
180
181
driver.get(pages.formPage);
182
WebElement uploadElement = driver.findElement(By.id("upload"));
183
assertThat(uploadElement.getAttribute("value")).isEmpty();
184
185
uploadElement.sendKeys(file.getAbsolutePath());
186
uploadElement.submit();
187
188
// Apparently on chrome we need to wait for the element to be gone if we're loading the same
189
// page again
190
wait.until(d -> d.findElements(By.id("upload")).isEmpty());
191
192
driver.get(pages.formPage);
193
uploadElement = driver.findElement(By.id("upload"));
194
assertThat(uploadElement.getAttribute("value")).isEmpty();
195
196
uploadElement.sendKeys(file.getAbsolutePath());
197
uploadElement.submit();
198
199
// If we get this far, then we're all good.
200
}
201
202
@Test
203
void testSendingKeyboardEventsShouldAppendTextInInputs() {
204
driver.get(pages.formPage);
205
WebElement element = driver.findElement(By.id("working"));
206
element.sendKeys("some");
207
String value = element.getAttribute("value");
208
assertThat(value).isEqualTo("some");
209
210
element.sendKeys(" text");
211
value = element.getAttribute("value");
212
assertThat(value).isEqualTo("some text");
213
}
214
215
@Test
216
@NotYetImplemented(SAFARI)
217
public void testSendingKeyboardEventsShouldAppendTextInInputsWithExistingValue() {
218
driver.get(pages.formPage);
219
WebElement element = wait.until(presenceOfElementLocated(By.id("inputWithText")));
220
element.sendKeys(". Some text");
221
String value = element.getAttribute("value");
222
223
assertThat(value).isEqualTo("Example text. Some text");
224
}
225
226
@Test
227
@NotYetImplemented(SAFARI)
228
public void testSendingKeyboardEventsShouldAppendTextInTextAreas() {
229
driver.get(pages.formPage);
230
WebElement element = driver.findElement(By.id("withText"));
231
232
element.sendKeys(". Some text");
233
String value = element.getAttribute("value");
234
235
assertThat(value).isEqualTo("Example text. Some text");
236
}
237
238
@Test
239
void testEmptyTextBoxesShouldReturnAnEmptyStringNotNull() {
240
driver.get(pages.formPage);
241
WebElement emptyTextBox = driver.findElement(By.id("working"));
242
assertThat(emptyTextBox.getAttribute("value")).isEmpty();
243
}
244
245
@Test
246
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
247
public void handleFormWithJavascriptAction() {
248
String url = appServer.whereIs("form_handling_js_submit.html");
249
driver.get(url);
250
WebElement element = driver.findElement(By.id("theForm"));
251
element.submit();
252
Alert alert = wait.until(alertIsPresent());
253
String text = alert.getText();
254
alert.accept();
255
256
assertThat(text).isEqualTo("Tasty cheese");
257
}
258
259
@Test
260
void testCanClickOnASubmitButton() {
261
checkSubmitButton("internal_explicit_submit");
262
}
263
264
@Test
265
void testCanClickOnASubmitButtonNestedSpan() {
266
checkSubmitButton("internal_span_submit");
267
}
268
269
@Test
270
void testCanClickOnAnImplicitSubmitButton() {
271
checkSubmitButton("internal_implicit_submit");
272
}
273
274
@Test
275
@Ignore(IE)
276
public void testCanClickOnAnExternalSubmitButton() {
277
checkSubmitButton("external_explicit_submit");
278
}
279
280
@Test
281
@Ignore(IE)
282
public void testCanClickOnAnExternalImplicitSubmitButton() {
283
checkSubmitButton("external_implicit_submit");
284
}
285
286
@Test
287
void canSubmitFormWithSubmitButtonIdEqualToSubmit() {
288
driver.get(pages.formPage);
289
driver.findElement(By.id("submit")).click();
290
wait.until(titleIs("We Arrive Here"));
291
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
292
}
293
294
@Test
295
void canSubmitFormWithSubmitButtonNameEqualToSubmit() {
296
String blank = appServer.create(new Page().withTitle("Submitted Successfully!"));
297
driver.get(
298
appServer.create(
299
new Page()
300
.withBody(
301
String.format("<form action='%s'>", blank),
302
" <input type='submit' name='submit' value='Submit'>",
303
"</form>")));
304
305
driver.findElement(By.name("submit")).submit();
306
wait.until(titleIs("Submitted Successfully!"));
307
}
308
309
private void checkSubmitButton(String buttonId) {
310
driver.get(appServer.whereIs("click_tests/html5_submit_buttons.html"));
311
String name = "Gromit";
312
313
driver.findElement(By.id("name")).sendKeys(name);
314
driver.findElement(By.id(buttonId)).click();
315
316
wait.until(titleIs("Submitted Successfully!"));
317
318
assertThat(driver.getCurrentUrl()).contains("name=" + name);
319
}
320
}
321
322