Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ClickScrollingTest.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.support.ui.ExpectedConditions.presenceOfElementLocated;
22
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
23
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
24
import static org.openqa.selenium.testing.drivers.Browser.IE;
25
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
26
27
import org.junit.jupiter.api.Test;
28
import org.openqa.selenium.testing.Ignore;
29
import org.openqa.selenium.testing.JupiterTestBase;
30
import org.openqa.selenium.testing.NotYetImplemented;
31
import org.openqa.selenium.testing.SwitchToTopAfterTest;
32
33
class ClickScrollingTest extends JupiterTestBase {
34
35
@Test
36
void testClickingOnAnchorScrollsPage() {
37
String scrollScript = "";
38
scrollScript += "var pageY;";
39
scrollScript += "if (typeof(window.pageYOffset) == 'number') {";
40
scrollScript += " pageY = window.pageYOffset;";
41
scrollScript += "} else {";
42
scrollScript += " pageY = document.documentElement.scrollTop;";
43
scrollScript += "}";
44
scrollScript += "return pageY;";
45
46
driver.get(pages.macbethPage);
47
48
driver.findElement(By.partialLinkText("last speech")).click();
49
50
Object x = ((JavascriptExecutor) driver).executeScript(scrollScript);
51
// Focusing on to click, but not actually following,
52
// the link will scroll it in to view, which is a few pixels further than 0
53
// According to documentation at
54
// https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset
55
// window.pageYOffset may not return integer value.
56
// With the following changes in below we are checking the type of returned object and assigning
57
// respectively
58
// the value of 'yOffset'
59
if (x instanceof Long) {
60
long yOffset = (Long) x;
61
assertThat(yOffset).describedAs("Did not scroll").isGreaterThan(300L);
62
} else if (x instanceof Double) {
63
double yOffset = (Double) x;
64
assertThat(yOffset).describedAs("Did not scroll").isGreaterThan(300.0);
65
}
66
}
67
68
@Test
69
void testShouldScrollToClickOnAnElementHiddenByOverflow() {
70
String url = appServer.whereIs("click_out_of_bounds_overflow.html");
71
driver.get(url);
72
73
WebElement link = driver.findElement(By.id("link"));
74
link.click();
75
}
76
77
@Test
78
void testShouldBeAbleToClickOnAnElementHiddenByOverflow() {
79
driver.get(appServer.whereIs("scroll.html"));
80
81
WebElement link = driver.findElement(By.id("line8"));
82
// This used to throw a MoveTargetOutOfBoundsException - we don't expect it to
83
link.click();
84
assertThat(driver.findElement(By.id("clicked")).getText()).isEqualTo("line8");
85
}
86
87
@Test
88
@Ignore(
89
value = FIREFOX,
90
reason = "horizontal scroll bar gets in the way",
91
issue = "https://github.com/mozilla/geckodriver/issues/2013")
92
public void testShouldBeAbleToClickOnAnElementHiddenByDoubleOverflow() {
93
driver.get(appServer.whereIs("scrolling_tests/page_with_double_overflow_auto.html"));
94
95
driver.findElement(By.id("link")).click();
96
wait.until(titleIs("Clicked Successfully!"));
97
}
98
99
@Test
100
void testShouldBeAbleToClickOnAnElementHiddenByYOverflow() {
101
driver.get(appServer.whereIs("scrolling_tests/page_with_y_overflow_auto.html"));
102
103
driver.findElement(By.id("link")).click();
104
wait.until(titleIs("Clicked Successfully!"));
105
}
106
107
@Test
108
@Ignore(value = IE, issue = "716")
109
@Ignore(
110
value = FIREFOX,
111
reason = "horizontal scroll bar gets in the way",
112
issue = "https://github.com/mozilla/geckodriver/issues/2013")
113
public void testShouldBeAbleToClickOnAnElementPartiallyHiddenByOverflow() {
114
driver.get(appServer.whereIs("scrolling_tests/page_with_partially_hidden_element.html"));
115
116
driver.findElement(By.id("btn")).click();
117
wait.until(titleIs("Clicked Successfully!"));
118
}
119
120
@Test
121
void testShouldNotScrollOverflowElementsWhichAreVisible() {
122
driver.get(appServer.whereIs("scroll2.html"));
123
WebElement list = driver.findElement(By.tagName("ul"));
124
WebElement item = list.findElement(By.id("desired"));
125
item.click();
126
long yOffset =
127
(Long) ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollTop;", list);
128
assertThat(yOffset).describedAs("Should not have scrolled").isZero();
129
}
130
131
@Test
132
@Ignore(
133
value = FIREFOX,
134
reason = "horizontal scroll bar gets in the way",
135
issue = "https://github.com/mozilla/geckodriver/issues/2013")
136
@NotYetImplemented(IE)
137
public void testShouldNotScrollIfAlreadyScrolledAndElementIsInView() {
138
driver.get(appServer.whereIs("scroll3.html"));
139
driver.findElement(By.id("button2")).click();
140
long scrollTop = getScrollTop();
141
driver.findElement(By.id("button1")).click();
142
assertThat(getScrollTop()).isEqualTo(scrollTop);
143
}
144
145
@Test
146
void testShouldBeAbleToClickRadioButtonScrolledIntoView() {
147
driver.get(appServer.whereIs("scroll4.html"));
148
driver.findElement(By.id("radio")).click();
149
// If we don't throw, we're good
150
}
151
152
@Test
153
@Ignore(value = IE, reason = "IE has special overflow handling")
154
@NotYetImplemented(SAFARI)
155
public void testShouldScrollOverflowElementsIfClickPointIsOutOfViewButElementIsInView() {
156
driver.get(appServer.whereIs("scroll5.html"));
157
driver.findElement(By.id("inner")).click();
158
assertThat(driver.findElement(By.id("clicked")).getText()).isEqualTo("clicked");
159
}
160
161
@SwitchToTopAfterTest
162
@Test
163
@NotYetImplemented(SAFARI)
164
@Ignore(
165
value = FIREFOX,
166
reason = "frame not scrolled into view",
167
issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1314462")
168
public void testShouldBeAbleToClickElementInAFrameThatIsOutOfView() {
169
driver.get(appServer.whereIs("scrolling_tests/page_with_frame_out_of_view.html"));
170
driver.switchTo().frame("frame");
171
WebElement element = driver.findElement(By.name("checkbox"));
172
element.click();
173
assertThat(element.isSelected()).isTrue();
174
}
175
176
@SwitchToTopAfterTest
177
@Test
178
void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {
179
driver.get(appServer.whereIs("scrolling_tests/page_with_scrolling_frame.html"));
180
driver.switchTo().frame("scrolling_frame");
181
WebElement element = driver.findElement(By.name("scroll_checkbox"));
182
element.click();
183
assertThat(element.isSelected()).isTrue();
184
}
185
186
@SwitchToTopAfterTest
187
@Test
188
@NotYetImplemented(SAFARI)
189
public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrameThatIsOutOfView() {
190
driver.get(appServer.whereIs("scrolling_tests/page_with_scrolling_frame_out_of_view.html"));
191
driver.switchTo().frame("scrolling_frame");
192
WebElement element = driver.findElement(By.name("scroll_checkbox"));
193
element.click();
194
assertThat(element.isSelected()).isTrue();
195
}
196
197
@SwitchToTopAfterTest
198
@Test
199
@Ignore(
200
value = FIREFOX,
201
reason = "horizontal scroll bar gets in the way",
202
issue = "https://github.com/mozilla/geckodriver/issues/2013")
203
public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrame() {
204
driver.get(appServer.whereIs("scrolling_tests/page_with_nested_scrolling_frames.html"));
205
driver.switchTo().frame("scrolling_frame");
206
driver.switchTo().frame("nested_scrolling_frame");
207
WebElement element = driver.findElement(By.name("scroll_checkbox"));
208
element.click();
209
assertThat(element.isSelected()).isTrue();
210
}
211
212
@SwitchToTopAfterTest
213
@Test
214
@Ignore(
215
value = FIREFOX,
216
reason = "horizontal scroll bar gets in the way",
217
issue = "https://github.com/mozilla/geckodriver/issues/2013")
218
@NotYetImplemented(SAFARI)
219
public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrameThatIsOutOfView() {
220
driver.get(
221
appServer.whereIs("scrolling_tests/page_with_nested_scrolling_frames_out_of_view.html"));
222
driver.switchTo().frame("scrolling_frame");
223
driver.switchTo().frame("nested_scrolling_frame");
224
WebElement element = driver.findElement(By.name("scroll_checkbox"));
225
element.click();
226
227
assertThat(element.isSelected()).isTrue();
228
}
229
230
@Test
231
void testShouldNotScrollWhenGettingElementSize() {
232
driver.get(appServer.whereIs("scroll3.html"));
233
long scrollTop = getScrollTop();
234
driver.findElement(By.id("button1")).getSize();
235
assertThat(getScrollTop()).isEqualTo(scrollTop);
236
}
237
238
private long getScrollTop() {
239
wait.until(presenceOfElementLocated(By.tagName("body")));
240
return (Long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollTop;");
241
}
242
243
@SwitchToTopAfterTest
244
@Test
245
@NotYetImplemented(SAFARI)
246
@Ignore(
247
value = FIREFOX,
248
reason = "frame not scrolled into view",
249
issue = "https://bugzilla.mozilla.org/show_bug.cgi?id=1314462")
250
public void testShouldBeAbleToClickElementInATallFrame() {
251
driver.get(appServer.whereIs("scrolling_tests/page_with_tall_frame.html"));
252
driver.switchTo().frame("tall_frame");
253
WebElement element = driver.findElement(By.name("checkbox"));
254
element.click();
255
assertThat(element.isSelected()).isTrue();
256
}
257
}
258
259