Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ClickTest.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.openqa.selenium.WaitingConditions.newWindowIsOpened;
22
import static org.openqa.selenium.WaitingConditions.pageSourceToContain;
23
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
24
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
25
import static org.openqa.selenium.testing.drivers.Browser.IE;
26
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
27
28
import java.util.Set;
29
import org.junit.jupiter.api.BeforeEach;
30
import org.junit.jupiter.api.Test;
31
import org.openqa.selenium.testing.Ignore;
32
import org.openqa.selenium.testing.JupiterTestBase;
33
import org.openqa.selenium.testing.NoDriverAfterTest;
34
import org.openqa.selenium.testing.NotYetImplemented;
35
import org.openqa.selenium.testing.SwitchToTopAfterTest;
36
37
class ClickTest extends JupiterTestBase {
38
39
@BeforeEach
40
public void setUp() {
41
driver.get(pages.clicksPage);
42
}
43
44
@Test
45
void testCanClickOnALinkAndFollowIt() {
46
driver.findElement(By.id("normal")).click();
47
48
wait.until(titleIs("XHTML Test Page"));
49
}
50
51
@Test
52
void testCanClickOnALinkThatOverflowsAndFollowIt() {
53
driver.findElement(By.id("overflowLink")).click();
54
55
wait.until(titleIs("XHTML Test Page"));
56
}
57
58
@Test
59
void testCanClickOnAnAnchorAndNotReloadThePage() {
60
((JavascriptExecutor) driver).executeScript("document.latch = true");
61
62
driver.findElement(By.id("anchor")).click();
63
64
Boolean samePage =
65
(Boolean) ((JavascriptExecutor) driver).executeScript("return document.latch");
66
67
assertThat(samePage).as("Latch was reset").isTrue();
68
}
69
70
@SwitchToTopAfterTest
71
@Test
72
void testCanClickOnALinkThatUpdatesAnotherFrame() {
73
driver.switchTo().frame("source");
74
75
driver.findElement(By.id("otherframe")).click();
76
driver.switchTo().defaultContent().switchTo().frame("target");
77
78
wait.until(pageSourceToContain("Hello WebDriver"));
79
}
80
81
@SwitchToTopAfterTest
82
@Test
83
void testElementsFoundByJsCanLoadUpdatesInAnotherFrame() {
84
driver.switchTo().frame("source");
85
86
WebElement toClick =
87
(WebElement)
88
((JavascriptExecutor) driver)
89
.executeScript("return document.getElementById('otherframe');");
90
toClick.click();
91
driver.switchTo().defaultContent().switchTo().frame("target");
92
93
wait.until(pageSourceToContain("Hello WebDriver"));
94
}
95
96
@SwitchToTopAfterTest
97
@Test
98
void testJsLocatedElementsCanUpdateFramesIfFoundSomehowElse() {
99
driver.switchTo().frame("source");
100
101
// Prime the cache of elements
102
driver.findElement(By.id("otherframe"));
103
104
// This _should_ return the same element
105
WebElement toClick =
106
(WebElement)
107
((JavascriptExecutor) driver)
108
.executeScript("return document.getElementById('otherframe');");
109
toClick.click();
110
driver.switchTo().defaultContent().switchTo().frame("target");
111
112
wait.until(pageSourceToContain("Hello WebDriver"));
113
}
114
115
@Test
116
void testCanClickOnAnElementWithTopSetToANegativeNumber() {
117
String page = appServer.whereIs("styledPage.html");
118
driver.get(page);
119
WebElement searchBox = driver.findElement(By.name("searchBox"));
120
searchBox.sendKeys("Cheese");
121
driver.findElement(By.name("btn")).click();
122
123
String log = driver.findElement(By.id("log")).getText();
124
assertThat(log).isEqualTo("click");
125
}
126
127
@Test
128
@NotYetImplemented(SAFARI)
129
public void testShouldSetRelatedTargetForMouseOver() {
130
driver.get(pages.javascriptPage);
131
132
driver.findElement(By.id("movable")).click();
133
134
String log = driver.findElement(By.id("result")).getText();
135
136
assertThat(log).isEqualTo("parent matches? true");
137
}
138
139
@Test
140
@NotYetImplemented(SAFARI)
141
public void testShouldClickOnFirstBoundingClientRectWithNonZeroSize() {
142
driver.findElement(By.id("twoClientRects")).click();
143
wait.until(titleIs("XHTML Test Page"));
144
}
145
146
@NoDriverAfterTest(failedOnly = true)
147
@Test
148
void testShouldOnlyFollowHrefOnce() {
149
driver.get(pages.clicksPage);
150
String current = driver.getWindowHandle();
151
Set<String> currentWindowHandles = driver.getWindowHandles();
152
153
try {
154
driver.findElement(By.id("new-window")).click();
155
String newWindowHandle = wait.until(newWindowIsOpened(currentWindowHandles));
156
driver.switchTo().window(newWindowHandle);
157
driver.close();
158
} finally {
159
driver.switchTo().window(current);
160
}
161
}
162
163
@Test
164
@NotYetImplemented(SAFARI)
165
public void testClickingLabelShouldSetCheckbox() {
166
driver.get(pages.formPage);
167
168
driver.findElement(By.id("label-for-checkbox-with-label")).click();
169
170
assertThat(driver.findElement(By.id("checkbox-with-label")).isSelected()).isTrue();
171
}
172
173
@Test
174
@NotYetImplemented(SAFARI)
175
public void testCanClickOnALinkWithEnclosedImage() {
176
driver.findElement(By.id("link-with-enclosed-image")).click();
177
178
wait.until(titleIs("XHTML Test Page"));
179
}
180
181
@Test
182
void testCanClickOnAnImageEnclosedInALink() {
183
driver.findElement(By.id("link-with-enclosed-image")).findElement(By.tagName("img")).click();
184
185
wait.until(titleIs("XHTML Test Page"));
186
}
187
188
@Test
189
@NotYetImplemented(SAFARI)
190
public void testCanClickOnALinkThatContainsTextWrappedInASpan() {
191
driver.findElement(By.id("link-with-enclosed-span")).click();
192
193
wait.until(titleIs("XHTML Test Page"));
194
}
195
196
@Test
197
@Ignore(
198
value = FIREFOX,
199
reason = "block can't be scrolled into view",
200
issue = "https://github.com/mozilla/geckodriver/issues/653")
201
@NotYetImplemented(SAFARI)
202
public void testCanClickOnALinkThatContainsEmbeddedBlockElements() {
203
driver.findElement(By.id("embeddedBlock")).click();
204
wait.until(titleIs("XHTML Test Page"));
205
}
206
207
@Test
208
void testCanClickOnAnElementEnclosedInALink() {
209
driver.findElement(By.id("link-with-enclosed-span")).findElement(By.tagName("span")).click();
210
211
wait.until(titleIs("XHTML Test Page"));
212
}
213
214
@Test
215
void testShouldBeAbleToClickOnAnElementInTheViewport() {
216
String url = appServer.whereIs("click_out_of_bounds.html");
217
218
driver.get(url);
219
WebElement button = driver.findElement(By.id("button"));
220
button.click();
221
}
222
223
@Test
224
@NotYetImplemented(SAFARI)
225
public void testClicksASurroundingStrongTag() {
226
driver.get(appServer.whereIs("ClickTest_testClicksASurroundingStrongTag.html"));
227
driver.findElement(By.tagName("a")).click();
228
wait.until(titleIs("XHTML Test Page"));
229
}
230
231
@Test
232
@Ignore(IE)
233
@Ignore(
234
value = FIREFOX,
235
reason = "imagemap can't be scrolled into view",
236
issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1502636")
237
@NotYetImplemented(SAFARI)
238
public void testCanClickAnImageMapArea() {
239
driver.get(appServer.whereIs("click_tests/google_map.html"));
240
driver.findElement(By.id("rectG")).click();
241
wait.until(titleIs("Target Page 1"));
242
243
driver.get(appServer.whereIs("click_tests/google_map.html"));
244
driver.findElement(By.id("circleO")).click();
245
wait.until(titleIs("Target Page 2"));
246
247
driver.get(appServer.whereIs("click_tests/google_map.html"));
248
driver.findElement(By.id("polyLE")).click();
249
wait.until(titleIs("Target Page 3"));
250
}
251
252
@Test
253
@Ignore(
254
value = FIREFOX,
255
reason = "Large element can't be scrolled into view",
256
issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1422272")
257
@NotYetImplemented(SAFARI)
258
public void testShouldBeAbleToClickOnAnElementGreaterThanTwoViewports() {
259
String url = appServer.whereIs("click_too_big.html");
260
driver.get(url);
261
262
WebElement element = driver.findElement(By.id("click"));
263
264
element.click();
265
266
wait.until(titleIs("clicks"));
267
}
268
269
@SwitchToTopAfterTest
270
@Test
271
@NotYetImplemented(SAFARI)
272
@Ignore(
273
value = FIREFOX,
274
reason =
275
"Clicking on element doesn't work if element is in iframe outside of viewport in Beta",
276
issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1937115")
277
public void testShouldBeAbleToClickOnAnElementInFrameGreaterThanTwoViewports() {
278
String url = appServer.whereIs("click_too_big_in_frame.html");
279
driver.get(url);
280
281
WebElement frame = driver.findElement(By.id("iframe1"));
282
driver.switchTo().frame(frame);
283
284
WebElement element = driver.findElement(By.id("click"));
285
element.click();
286
287
wait.until(titleIs("clicks"));
288
}
289
290
@Test
291
void testShouldBeAbleToClickOnRTLLanguageLink() {
292
String url = appServer.whereIs("click_rtl.html");
293
driver.get(url);
294
295
WebElement element = driver.findElement(By.id("ar_link"));
296
element.click();
297
298
wait.until(titleIs("clicks"));
299
}
300
301
@Test
302
void testShouldBeAbleToClickOnLinkInAbsolutelyPositionedFooter() {
303
String url = appServer.whereIs("fixedFooterNoScroll.html");
304
driver.get(url);
305
306
WebElement element = driver.findElement(By.id("link"));
307
element.click();
308
309
wait.until(titleIs("XHTML Test Page"));
310
}
311
312
@Test
313
void testShouldBeAbleToClickOnLinkInAbsolutelyPositionedFooterInQuirksMode() {
314
String url = appServer.whereIs("fixedFooterNoScrollQuirksMode.html");
315
driver.get(url);
316
317
WebElement element = driver.findElement(By.id("link"));
318
element.click();
319
320
wait.until(titleIs("XHTML Test Page"));
321
}
322
323
@Test
324
void testShouldBeAbleToClickOnLinksWithNoHrefAttribute() {
325
driver.get(pages.javascriptPage);
326
327
WebElement element = driver.findElement(By.linkText("No href"));
328
element.click();
329
330
wait.until(titleIs("Changed"));
331
}
332
333
@Test
334
void testShouldBeAbleToClickOnALinkThatWrapsToTheNextLine() {
335
driver.get(appServer.whereIs("click_tests/link_that_wraps.html"));
336
337
driver.findElement(By.id("link")).click();
338
339
wait.until(titleIs("Submitted Successfully!"));
340
}
341
342
@Test
343
void testShouldBeAbleToClickOnASpanThatWrapsToTheNextLine() {
344
driver.get(appServer.whereIs("click_tests/span_that_wraps.html"));
345
346
driver.findElement(By.id("span")).click();
347
348
wait.until(titleIs("Submitted Successfully!"));
349
}
350
351
@Test
352
void clickingOnADisabledElementIsANoOp() {
353
driver.get(appServer.whereIs("click_tests/disabled_element.html"));
354
355
driver.findElement(By.name("disabled")).click(); // Should not throw
356
}
357
}
358
359