Test Diary

Archives


Social Profile


Recent Comments


My Seventh Selenium WebDriver Test (Page Scroll)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In our previous posts, we covered topics on selenium frames, tables, forms, waits and text fields. Now, we will take a look at page scrolling.

A lot of times, I often hear testers complain that they tried to click on a link or a button during their tests and they couldn’t. In most situations, they get error messages like this “Element is not clickable at point (511, 17.5). Other element would receive the click:“.

To prevent us from having such issues we will take a look at the getLocation() method which selenium provides to solve this challenge. The getLocation() method is used to find a point, containing the location of the top left-hand corner of an element. This  method contains the X,Y coordinate of any element on a webpage. For example, in the error message above you will notice that it saysElement is not clickable at point (511, 17.5)”. These are the coordinates for a particular element that selenium was trying to click on, but couldn’t. Therefore, in such scenarios, we need to get the X, Y coordinates of that element then tell selenium to scroll down or up based on those coordinates. We can do this, by chaining the getLocation() method with the getY() method or getX() method.

Example: getLocation().getY()!!!!

Now, once we get our coordinates, we need to be able to scroll to it. For this tutorial, we will be using the JavaScript Executor interface (don’t be scared we won’t be using any core Java script language, it’s just a simple interface and very easy to understand).  This interface helps us to execute some java script on our page. So with a command like this  “executeScript(“window.scrollBy(“X”,” Y”)”, “”)”  we can easily insert our X and Y coordinates and selenium will go to those coordinates on the webPage.

Without further ado, let’s follow the steps below and then dive into our code to understand how we test/implement a page scroll.

  1. Navigate to the sample test page.
  2. Scroll down to the “Open page in the same window” link
  3. Click on the link
Seventh Test (Page Scroll) 


import org.openqa.selenium.By;

// we have imported the Javascript Executor

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

public class TestdiaryPageScroll {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";

driver.get(contactUrl);

//wait until Selenium can find the link with locator Linktext "Open page in a new window" on the test page.

(new WebDriverWait(driver, 10))

.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Open page in the same window")));

 

//Point Selenium to the Element you want to scroll to and get it's Location

//Store location with Y coordinates in the variable hyperlinkYCoordinate of type integer

int hyperlinkYCoordinate = driver.findElement(By.linkText("Open page in the same window")).getLocation().getY();

 

//Store location with X coordinates in the variable hyperlinkYCoordinate of type integer

int hyperlinkXCoordinate = driver.findElement(By.linkText("Open page in the same window")).getLocation().getX();

 

// Use Java Script Executor to scroll down the WebPage to the position where the element is

JavascriptExecutor jsexecutor = (JavascriptExecutor) driver;

 

// parse the X and Y coordinates from the above into the execute Script method with the following String

jsexecutor.executeScript("window.scrollBy(" + hyperlinkXCoordinate + "," + hyperlinkYCoordinate + ")", "");

// wait until element 'linkText("Open page in the same window")' is clickable
(new WebDriverWait(driver, 100)).until(ExpectedConditions.elementToBeClickable(By.linkText("Open page in the same window")));

// click on the link

driver.findElement(By.linkText("Open page in the same window")).click();

driver.close();

}

}

In conclusion, we have looked at how to scroll up or down a webpage to click on a particular element.  This approach can be used in your future tests. You can download or clone this test from the Testdiary Github.

Click: Page Scroll

Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.