-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Wheel examples #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Wheel examples #1010
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| using System; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium; | ||
| using OpenQA.Selenium.Chrome; | ||
|
|
||
| namespace SeleniumDocs.GettingStarted | ||
| { | ||
| [TestClass] | ||
| public class FirstScriptTest | ||
| { | ||
| public IWebDriver driver; | ||
|
|
||
| [TestInitialize] | ||
| public void Setup() | ||
| { | ||
| driver = new ChromeDriver(); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void Teardown() | ||
| { | ||
| driver?.Quit(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ShouldAllowScrollingToAnElement() | ||
| { | ||
| driver.Navigate().GoToUrl("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"); | ||
|
|
||
| driver.Url = scrollFrameOutOfViewport; | ||
| IWebElement iframe = driver.FindElement(By.TagName("iframe")); | ||
|
|
||
| Assert.IsFalse(IsInViewport(iframe)); | ||
|
|
||
| new Actions(driver).ScrollToElement(iframe).Build().Perform(); | ||
|
|
||
| Assert.IsTrue(IsInViewport(iframe)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ShouldAllowScrollingFromViewportByGivenAmount() | ||
| { | ||
| driver.Navigate().GoToUrl("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"); | ||
| IWebElement footer = driver.FindElement(By.TagName("footer")); | ||
| int deltaY = footer.Location.Y; | ||
|
|
||
| new Actions(driver).ScrollByAmount(0, deltaY).Build().Perform(); | ||
|
|
||
| Assert.IsTrue(IsInViewport(footer)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ShouldScrollFromElementByGivenAmount() | ||
| { | ||
| driver.Navigate().GoToUrl("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"); | ||
| IWebElement iframe = driver.FindElement(By.TagName("iframe")); | ||
| WheelInputDevice.ScrollOrigin scrollOrigin = new WheelInputDevice.ScrollOrigin | ||
| { | ||
| Element = iframe | ||
| }; | ||
|
|
||
| new Actions(driver).ScrollFromOrigin(scrollOrigin, 0, 200).Build().Perform(); | ||
|
|
||
| driver.SwitchTo().Frame(iframe); | ||
| IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox")); | ||
| Assert.IsTrue(IsInViewport(checkbox)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ShouldAllowScrollingFromElementByGivenAmountWithOffset() | ||
| { | ||
| driver.Navigate().GoToUrl("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"); | ||
| IWebElement footer = driver.FindElement(By.TagName("footer")); | ||
| var scrollOrigin = new WheelInputDevice.ScrollOrigin | ||
| { | ||
| Element = footer, | ||
| XOffset = 0, | ||
| YOffset = -50 | ||
| }; | ||
|
|
||
| new Actions(driver).ScrollFromOrigin(scrollOrigin, 0, 200).Build().Perform(); | ||
|
|
||
| IWebElement iframe = driver.FindElement(By.TagName("iframe")); | ||
| driver.SwitchTo().Frame(iframe); | ||
| IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox")); | ||
| Assert.IsTrue(IsInViewport(checkbox)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ShouldAllowScrollingFromViewportByGivenAmountFromOrigin() | ||
| { | ||
| driver.Navigate().GoToUrl("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html"); | ||
| var scrollOrigin = new WheelInputDevice.ScrollOrigin | ||
| { | ||
| Viewport = true, | ||
| XOffset = 10, | ||
| YOffset = 10 | ||
| }; | ||
|
|
||
| new Actions(driver).ScrollFromOrigin(scrollOrigin, 0, 200).Build().Perform(); | ||
|
|
||
| IWebElement iframe = driver.FindElement(By.TagName("iframe")); | ||
| driver.SwitchTo().Frame(iframe); | ||
| IWebElement checkbox = driver.FindElement(By.Name("scroll_checkbox")); | ||
| Assert.IsTrue(IsInViewport(checkbox)); | ||
| } | ||
|
|
||
| private bool IsInViewport(IWebElement 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"; | ||
| IJavaScriptExecutor javascriptDriver = this.driver as IJavaScriptExecutor; | ||
|
|
||
| return (bool)javascriptDriver.ExecuteScript(script, element); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package dev.selenium.actions_api; | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.JavascriptExecutor; | ||
| import org.openqa.selenium.WebDriver; | ||
| import org.openqa.selenium.WebElement; | ||
| import org.openqa.selenium.chrome.ChromeDriver; | ||
| import org.openqa.selenium.interactions.Actions; | ||
| import org.openqa.selenium.interactions.WheelInput; | ||
|
|
||
| public class WheelTest { | ||
| public WebDriver driver; | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| driver = new ChromeDriver(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void quit() { | ||
| driver.quit(); | ||
| } | ||
|
|
||
| @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")); | ||
|
|
||
| Assertions.assertFalse(inViewport(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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const {Builder, By, Key, until} = require('selenium-webdriver'); | ||
|
|
||
| (async function scrolling() { | ||
| try { | ||
| let driver = await new Builder().forBrowser('chrome').build(); | ||
|
|
||
| await driver.quit(); | ||
| } catch (error) { | ||
| console.log(error) | ||
| } | ||
| })(); |
103 changes: 103 additions & 0 deletions
103
examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package dev.selenium.getting_started | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.JavascriptExecutor; | ||
| import org.openqa.selenium.WebDriver; | ||
| import org.openqa.selenium.WebElement; | ||
| import org.openqa.selenium.chrome.ChromeDriver; | ||
| import org.openqa.selenium.interactions.Actions; | ||
| import org.openqa.selenium.interactions.WheelInput; | ||
|
|
||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class ScrollTest { | ||
| private lateinit var driver: WebDriver | ||
|
|
||
| @BeforeEach | ||
| fun setupBrowser() { | ||
| driver = ChromeDriver() | ||
| } | ||
|
|
||
| @AfterEach | ||
| fun cleanupBrowser() { | ||
| driver.quit() | ||
| } | ||
|
|
||
| @Test | ||
| fun 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")); | ||
|
|
||
| Assertions.assertFalse(inViewport(iframe)); | ||
|
|
||
| new Actions(driver).scrollToElement(iframe).perform(); | ||
|
|
||
| Assertions.assertTrue(inViewport(iframe)); | ||
| } | ||
|
|
||
| @Test | ||
| fun 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 | ||
| fun 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 | ||
| fun 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 | ||
| fun 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)); | ||
| } | ||
|
|
||
| fun 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
executeScript will return Boolean as Object, seems no need to convert as boolean?