-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
WheelTest.java
94 lines (75 loc) · 3.8 KB
/
WheelTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dev.selenium.actions_api;
import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.WheelInput;
public class WheelTest extends BaseChromeTest {
@Test
public void shouldScrollToElement() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
WebElement iframe = driver.findElement(By.tagName("iframe"));
new Actions(driver)
.scrollToElement(iframe)
.perform();
Assertions.assertTrue(inViewport(iframe));
}
@Test
public void shouldScrollFromViewportByGivenAmount() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
WebElement footer = driver.findElement(By.tagName("footer"));
int deltaY = footer.getRect().y;
new Actions(driver)
.scrollByAmount(0, deltaY)
.perform();
Assertions.assertTrue(inViewport(footer));
}
@Test
public void shouldScrollFromElementByGivenAmount() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
WebElement iframe = driver.findElement(By.tagName("iframe"));
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(iframe);
new Actions(driver)
.scrollFromOrigin(scrollOrigin, 0, 200)
.perform();
driver.switchTo().frame(iframe);
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
Assertions.assertTrue(inViewport(checkbox));
}
@Test
public void shouldScrollFromElementByGivenAmountWithOffset() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
WebElement footer = driver.findElement(By.tagName("footer"));
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, -50);
new Actions(driver)
.scrollFromOrigin(scrollOrigin,0, 200)
.perform();
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
Assertions.assertTrue(inViewport(checkbox));
}
@Test
public void shouldScrollFromViewportByGivenAmountFromOrigin() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html");
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(10, 10);
new Actions(driver)
.scrollFromOrigin(scrollOrigin, 0, 200)
.perform();
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
Assertions.assertTrue(inViewport(checkbox));
}
private boolean inViewport(WebElement element) {
String script =
"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"
+ "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"
+ "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n"
+ "window.pageYOffset&&t+o>window.pageXOffset";
return (boolean) ((JavascriptExecutor) driver).executeScript(script, element);
}
}