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