Selenium WebDriver- Scrolling a web page

In this section, you will learn how to scroll down or up in order to display additional information present on the web page.

Let us consider a test case in which we will automate the following scenarios:

  • Invoke Firefox browser
  • Open URL: www.rookienerd.com
  • Scroll down through the web page in order to show the Java Technology section

We will create our test case step by step in order to give you a complete understanding of how to scroll a web page using "scrollBy" method of JavaScript.

Step1. Launch Eclipse IDE and open the existing test suite "Demo_Test" which we have created in earlier sessions of this tutorial.

Step2. Right click on the "src" folder and create a new Class File from New >Class.

Selenium WebDriver- Scrolling a web page

Give your Class name as "Scroll_Test" and click on "Finish" button.

Selenium WebDriver- Scrolling a web page

Step3. Let's get to the coding ground.

  • To invoke Firefox browser, we need to download Gecko driver and set the system property for Gecko driver. We have already discussed this in earlier sessions of this tutorial. You can refer "Running test on Firefox Browser" to learn how to download and set System property for Firefox driver.

Here is the sample code to set system property for Gecko driver:

snippet
// System Property for Gecko Driver 
	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
  • After that we have to initialize Gecko Driver using Desired Capabilities Class.

Here is the sample code to initialize gecko driver using DesiredCapabilities class.

snippet
// Initialize Gecko Driver using Desired Capabilities Class
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability("marionette",true);
	WebDriver driver= new FirefoxDriver(capabilities);

Combining both of the above code blocks, we will get the code snippet to launch Firefox browser.

snippet
// System Property for Gecko Driver 
	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
		
	 // Initialize Gecko Driver using Desired Capabilities Class
	DesiredCapabilities capabilities = DesiredCapabilities.firefox();
	capabilities.setCapability("marionette",true);
	WebDriver driver= new FirefoxDriver(capabilities);
  • After that we need to write the code which will automate our second test scenario (navigate to the desired URL)

Here is the sample code to navigate to the desired URL:

snippet
// Launch Website
driver.navigate().to("www.rookienerd.com");

The complete code till now will look something like this:

snippet
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Scroll_Test {

	public static void main(String[] args) {
		
	// System Property for Gecko Driver 
		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
			
	// Initialize Gecko Driver using Desired Capabilities Class
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
			capabilities.setCapability("marionette",true);
			WebDriver driver= new FirefoxDriver(capabilities);
		

      // Launch Website
driver.navigate().to("www.rookienerd.com"); 
	
	}

}

Step4. To automate our third test scenario, we need to write the code which will scroll down the webpage in order to show the Java Technology section present on the rookienerd website.

To scroll a web page, we have to use the scrollBy method of JavaScript. For executing the JavaScript method we will use JavaScript executor. The scrollBy method takes two parameters one each for horizontal and vertical scroll in terms of pixels.

snippet
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollBy(0, 4500)");

Thus, our final test script will look something like this:

snippet
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Scroll_Test {

	public static void main(String[] args) {
		
		// System Property for Gecko Driver 
		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
			
		// Initialize Gecko Driver using Desired Capabilities Class
			DesiredCapabilities capabilities = DesiredCapabilities.firefox();
			capabilities.setCapability("marionette",true);
			WebDriver driver= new FirefoxDriver(capabilities);
		

		// Launch Website
		driver.navigate().to("www.rookienerd.com"); 

		//Scroll down the webpage by 4500 pixels
		JavascriptExecutor js = (JavascriptExecutor)driver;
		js.executeScript("scrollBy(0, 4500)");

	}
}

Upon execution, the above test script will launch the Firefox browser and automate all the test scenarios.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +