Thursday 4 February 2016

How to perform horizantal scroll bar operations in selenium web driver:

Performing horizontal scroll bar operations in selenium web driver:
===================================================

We can perform scroll operations by using JavascriptExecutor interface.

see the below example.

-----------------------------

package blog;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class ScrollingHorizantal {

WebDriver webDriver;
JavascriptExecutor js;

@BeforeSuite
public void launchingIEBrowser() {

System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new InternetExplorerDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@Test
public void testScript() throws InterruptedException {
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();

//scroling Horizantal Right
js = (JavascriptExecutor) webDriver;
js.executeScript("window.scrollBy(2000,0)", "");

Thread.sleep(3000);

//scroling Horizantal Left
js = (JavascriptExecutor) webDriver;
js.executeScript("window.scrollBy(-2000,0)", "");
  

}

@AfterSuite
public void closeBrowser() {
webDriver.quit();

}


}

2 comments: