Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ClearTest.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.testing.drivers.Browser.CHROME;
23
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
24
import static org.openqa.selenium.testing.drivers.Browser.IE;
25
26
import org.junit.jupiter.api.Test;
27
import org.openqa.selenium.testing.Ignore;
28
import org.openqa.selenium.testing.JupiterTestBase;
29
import org.openqa.selenium.testing.NotYetImplemented;
30
31
class ClearTest extends JupiterTestBase {
32
33
@Test
34
void testWritableTextInputShouldClear() {
35
driver.get(pages.readOnlyPage);
36
WebElement element = driver.findElement(By.id("writableTextInput"));
37
element.clear();
38
assertThat(element.getAttribute("value")).isEmpty();
39
}
40
41
@Test
42
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
43
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
44
void testTextInputShouldNotClearWhenDisabled() {
45
driver.get(pages.readOnlyPage);
46
WebElement element = driver.findElement(By.id("textInputNotEnabled"));
47
assertThat(element.isEnabled()).isFalse();
48
assertThatExceptionOfType(InvalidElementStateException.class).isThrownBy(element::clear);
49
}
50
51
@Test
52
void testTextInputShouldNotClearWhenReadOnly() {
53
driver.get(pages.readOnlyPage);
54
WebElement element = driver.findElement(By.id("readOnlyTextInput"));
55
assertThatExceptionOfType(InvalidElementStateException.class).isThrownBy(element::clear);
56
}
57
58
@Test
59
void testWritableTextAreaShouldClear() {
60
driver.get(pages.readOnlyPage);
61
WebElement element = driver.findElement(By.id("writableTextArea"));
62
element.clear();
63
assertThat(element.getAttribute("value")).isEmpty();
64
}
65
66
@Test
67
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
68
@Ignore(value = EDGE, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
69
void testTextAreaShouldNotClearWhenDisabled() {
70
driver.get(pages.readOnlyPage);
71
WebElement element = driver.findElement(By.id("textAreaNotEnabled"));
72
assertThatExceptionOfType(InvalidElementStateException.class).isThrownBy(element::clear);
73
}
74
75
@Test
76
void testTextAreaShouldNotClearWhenReadOnly() {
77
driver.get(pages.readOnlyPage);
78
WebElement element = driver.findElement(By.id("textAreaReadOnly"));
79
assertThatExceptionOfType(InvalidElementStateException.class).isThrownBy(element::clear);
80
}
81
82
@Test
83
void testContentEditableAreaShouldClear() {
84
driver.get(pages.readOnlyPage);
85
WebElement element = driver.findElement(By.id("content-editable"));
86
element.clear();
87
assertThat(element.getText()).isEmpty();
88
}
89
90
@Test
91
void shouldBeAbleToClearNoTypeInput() {
92
shouldBeAbleToClearInput(By.name("no_type"), "input with no type");
93
}
94
95
@Test
96
void shouldBeAbleToClearNumberInput() {
97
shouldBeAbleToClearInput(By.name("number_input"), "42");
98
}
99
100
@Test
101
void shouldBeAbleToClearEmailInput() {
102
shouldBeAbleToClearInput(By.name("email_input"), "admin@localhost");
103
}
104
105
@Test
106
void shouldBeAbleToClearPasswordInput() {
107
shouldBeAbleToClearInput(By.name("password_input"), "qwerty");
108
}
109
110
@Test
111
void shouldBeAbleToClearSearchInput() {
112
shouldBeAbleToClearInput(By.name("search_input"), "search");
113
}
114
115
@Test
116
void shouldBeAbleToClearTelInput() {
117
shouldBeAbleToClearInput(By.name("tel_input"), "911");
118
}
119
120
@Test
121
void shouldBeAbleToClearTextInput() {
122
shouldBeAbleToClearInput(By.name("text_input"), "text input");
123
}
124
125
@Test
126
void shouldBeAbleToClearUrlInput() {
127
shouldBeAbleToClearInput(By.name("url_input"), "https://selenium.dev/");
128
}
129
130
@Test
131
public void shouldBeAbleToClearRangeInput() {
132
shouldBeAbleToClearInput(By.name("range_input"), "42", "50");
133
}
134
135
@Test
136
@NotYetImplemented(IE)
137
public void shouldBeAbleToClearColorInput() {
138
shouldBeAbleToClearInput(By.name("color_input"), "#00ffff", "#000000");
139
}
140
141
@Test
142
public void shouldBeAbleToClearDateInput() {
143
shouldBeAbleToClearInput(By.name("date_input"), "2017-11-22");
144
}
145
146
@Test
147
void shouldBeAbleToClearDatetimeInput() {
148
shouldBeAbleToClearInput(By.name("datetime_input"), "2017-11-22T11:22");
149
}
150
151
@Test
152
public void shouldBeAbleToClearDatetimeLocalInput() {
153
shouldBeAbleToClearInput(By.name("datetime_local_input"), "2017-11-22T11:22");
154
}
155
156
@Test
157
public void shouldBeAbleToClearTimeInput() {
158
shouldBeAbleToClearInput(By.name("time_input"), "11:22");
159
}
160
161
@Test
162
public void shouldBeAbleToClearMonthInput() {
163
shouldBeAbleToClearInput(By.name("month_input"), "2017-11");
164
}
165
166
@Test
167
public void shouldBeAbleToClearWeekInput() {
168
shouldBeAbleToClearInput(By.name("week_input"), "2017-W47");
169
}
170
171
private void shouldBeAbleToClearInput(By locator, String oldValue) {
172
shouldBeAbleToClearInput(locator, oldValue, "");
173
}
174
175
private void shouldBeAbleToClearInput(By locator, String oldValue, String clearedValue) {
176
driver.get(appServer.whereIs("inputs.html"));
177
WebElement element = driver.findElement(locator);
178
assertThat(element.getAttribute("value")).isEqualTo(oldValue);
179
element.clear();
180
assertThat(element.getAttribute("value")).isEqualTo(clearedValue);
181
}
182
}
183
184