Thursday, 30 July 2015

Scroll up/down using javaScript


package interview_practice;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ScrollDown_JavaScript {
     WebDriver driver;
@BeforeTest
 public void setup() throws Exception {
      driver = new FirefoxDriver();
      driver.manage().window().maximize();
     }
//@Test(priority=0) //working fine
public void Scroll_Page_usingJavaScript()
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
     //To scroll down web page by 600 pixels In x(vertical) direction. 
      //You can y parameter to scroll page In horizontal direction
    JavascriptExecutor javascript = (JavascriptExecutor) driver;
    javascript.executeScript("window.scrollBy(0,600)", "");
     //To scroll up web page by 300 pixels In x(vertical) direction.
      javascript.executeScript("window.scrollBy(0,-300)", "");
   
    }
//@Test(priority=1) //working fine
public void Scroll_totalDown_usingJavaScript() throws AWTException
{
    driver.get("http://googlet.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    //Scroll down to bottom of the page.
      JavascriptExecutor javascript = (JavascriptExecutor) driver;
      javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", ""); 
   
    }

//Scroll till element.
//@Test(priority=2)  //working fine
public void Scroll_till_element_usingJavaScript() throws AWTException
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    JavascriptExecutor je = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(By.xpath("//div[@id='dragdiv']"));
    je.executeScript("arguments[0].scrollIntoView(true);",element);
   
    }
@Test(priority=3)   //pending
public void Scroll_window_usingRobotKeys() throws AWTException
{
    driver.get("http://google.com");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
   

   
    }


}

No comments:

Post a Comment