Introduction
Welcome to part 2 of the Selenium scrolling tutorial. Please follow the directions in part 1 to set up your project.
In part 1, we learned how to let Selenium decide how it wants to scroll to elements. In this part 2, we will learn about a couple more scrolling use cases, which includes scrolling by the amount of pixels.
Scrolling Along The Y Axis
In Selenium scripts, a common use case is scrolling by an amount of pixels vertically. To do this, you can use the method scrollByAmount()
by the Actions API. For example, if you want to scroll down by 10 pixels and then 500 pixels, then you can use the code below.
@Test
void scrollByDown() throws InterruptedException {
driver.get("http://localhost:" + port + "/start");
new Actions(driver)
.scrollByAmount(0, 10)
.perform();
new Actions(driver)
.scrollByAmount(0, 500)
.perform();
Thread.sleep(5000);
}
The example shows how to scroll down, but if you want to scroll up, simply use a negative y delta instead. The test below initially scrolls all the way to the end, and then all the way back up.
@Test
void scrollByUp() throws InterruptedException {
driver.get("http://localhost:" + port + "/start");
// Scrolls to end of page
new Actions(driver)
.sendKeys(Keys.END)
.perform();
Thread.sleep(2000);
new Actions(driver)
.scrollByAmount(0, -1000)
.perform();
Thread.sleep(5000);
}
Scrolling Along The X Axis
Similarly, if you want to scroll horizontally, you can simply do that by changing the x delta to another value besides 0. The example below shows how scrolling along the X axis looks like.
@Test
void scrollByXDown() throws InterruptedException {
driver.get("http://localhost:" + port + "/start");
new Actions(driver)
.scrollByAmount(100, 0)
.perform();
Thread.sleep(1000);
new Actions(driver)
.scrollByAmount(100, 0)
.perform();
Thread.sleep(5000);
}
Scrolling With Keyboard
Some users with certain physical disabilities might not be able to use the mouse for scrolling. So it might be beneficial to test whether the browser scrolls correctly using the commands sent by the keyboard. One way to achieve this is by sending virtual keys with Selenium.
To send keys to the browser, you can use the Actions API’s sendKeys()
method, passing in one of the enums in this list.
Below are some examples of Selenium tests sending scrolling keys to the browser.
-
Page Down Key:
@Test void scrollByPgDnKey() throws InterruptedException { driver.get("http://localhost:" + port + "/start"); new Actions(driver) .sendKeys(Keys.PAGE_DOWN) .perform(); Thread.sleep(1000); }
-
Page Up Key.
@Test void scrollByPgUpKey() throws InterruptedException { driver.get("http://localhost:" + port + "/start"); // Scrolls to end of page new Actions(driver) .sendKeys(Keys.END) .perform(); Thread.sleep(1000); new Actions(driver) .sendKeys(Keys.PAGE_UP) .perform();; Thread.sleep(1000); }
-
Arrow Down Key.
@Test void scrollByArrowDnKey() throws InterruptedException { driver.get("http://localhost:" + port + "/start"); new Actions(driver) .sendKeys(Keys.ARROW_DOWN); Thread.sleep(1000); new Actions(driver) .sendKeys(Keys.ARROW_DOWN); Thread.sleep(1000); }
-
Arrow Up Key.
@Test void scrollByArrowUpKey() throws InterruptedException { driver.get("http://localhost:" + port + "/start"); // Scrolls to end of page new Actions(driver) .sendKeys(Keys.END) .perform(); new Actions(driver) .sendKeys(Keys.ARROW_UP); Thread.sleep(1000); new Actions(driver) .sendKeys(Keys.ARROW_UP); Thread.sleep(1000); }
Summary
We have learned a couple of different ways to perform scrolling automation on the browser. The full project code can be found at https://github.com/dmitrilc/Daniweb_Selenium/tree/scrolling.
Please ignore my use of Thread.sleep()
in the tutorial. I only use it to see what Selenium is doing because it runs really fast.