Automation QA Testing Course Content

How To Scroll the Pages in Selenium WebDriver

SCROLLING THE PAGES
==================================================================
-------------------------------------------------------------------------------
1)Using JavascriptExecutor interface
//launch the browser
WebDriver driver=new FirefoxDriver();

//create a variable for JavscriptExecutor and assign webdriver varaible
JavascriptExecutor jsx=(JavascriptExecutor)driver;
//scrollDown
//jsx.executeScript("window.scrollBy(horizontal,verticaldown)","");
//vertically scrollUP
jsx.executeScript("window.scrollBy(horizontal,-verticaldown)","");

how can you scroll until the visibility of particular element.

jsx.executeScript("arguments[0].scrollIntoView(true);",elelementName);
-----------------------------------------------------------------------------------
2)scrolling the page using Keys enum
driver.findElement(By.localtor("value")).sendkeys(keys.keyname);
scroll down
driver.findElement(By.tagName("body")).sendkeys(keys.DOWN)

Scroll Up
driver.findElement(By.tagName("body")).sendkeys(keys.UP)

=================================================================
PROGRAM:
package dropdownprograms;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ScrollAndTakesScreenshot {

static WebDriver driver = null;
static WebDriverWait wait = null;

public static void main(String[] args) throws InterruptedException, IOException {
// 1 set the chromdriver.exe file path System.setproperty(p1,p2)

System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome99\\chromedriver_win32\\chromedriver.exe");

// interface variablename=new classname();
driver = new ChromeDriver();

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

// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

// create WebDriverWaitobject
wait = new WebDriverWait(driver, Duration.ofMillis(30000));
// open the rediff.com
driver.get("https://www.amazon.in/");

// verify the page title
wait.until(ExpectedConditions.titleContains("Amazon.in"));

// identify the categories dropdown.
WebElement searchEditbox = driver.findElement(By.id("twotabsearchtextbox"));

// type the value in search editbox
searchEditbox.sendKeys("headphones");

// submiting on the searchEditbox
searchEditbox.submit();

wait.until(ExpectedConditions.titleContains("Amazon.in : headphones"));
wait.until(ExpectedConditions
.presenceOfElementLocated(By.xpath("//div[contains(@class,'s-breadcrumb')]/div/div")));

// scroll for few pixels
JavascriptExecutor jsx = (JavascriptExecutor) driver;
// scrolldown
jsx.executeScript("window.scrollBy(0,3500)", "");
// takes screenshot of entire page
captureScreenshot("scrolldownusingJSExecutor");
Thread.sleep(2000);
// scrollUP
jsx.executeScript("window.scrollBy(0,-3000)", "");
// takes screenshot of entire page
captureScreenshot("scrollUPUsingJSExecutor");
Thread.sleep(2000);
//scroll until the element is visible
WebElement navBackToTop=driver.findElement(By.id("navBackToTop"));
jsx.executeScript("arguments[0].scrollIntoView(true);", navBackToTop);
captureScreenshot("scrollForNavbackToTopElmnt");
Thread.sleep(2000);
//click on navBackToTop button
navBackToTop.click();
//scroll based on Keys Enum
for(int i=1;i<=10;i++) {
driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN);
}
captureScreenshot("scrollDOWNUsingKEYSDOWNKey");
Thread.sleep(2000);
//scrollup using key UP
for(int i=1;i<=10;i++) {
driver.findElement(By.tagName("body")).sendKeys(Keys.UP);
}
captureScreenshot("scrollUPUsingKEYSUPKey");
Thread.sleep(2000);
//take a screemshot of amazon logo
WebElement amaonlogo=driver.findElement(By.cssSelector("a#nav-logo-sprites"));
captureScreenshot(amaonlogo, "AmazonLogoScreenshot");
Thread.sleep(2000);
driver.quit();

}

/**
* Taking the entire browser screenshot
* @param screenName
* @throws IOException
*/
private static void captureScreenshot(String screenName) throws IOException {

// take screenshot
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// create Object for Date class
Date d = new Date();
screenName = screenName + "-" + d.toString().replace(":", "-").replace(" ", "-") + ".jpg";

// copy the file name under project directory
FileUtils.copyFile(src, new File(System.getProperty("user.dir") + "\\src\\screenshots\\" + screenName));

}

/**
* taking the element screenshot
* @param element
* @param screenName
* @throws IOException
*/
private static void captureScreenshot(WebElement element, String screenName) throws IOException {

// take screenshot
File src = ((TakesScreenshot) element).getScreenshotAs(OutputType.FILE);

// create Object for Date class
Date d = new Date();
screenName = screenName + "-" + d.toString().replace(":", "-").replace(" ", "-") + ".jpg";

// copy the file name under project directory
FileUtils.copyFile(src, new File(System.getProperty("user.dir") + "\\src\\screenshots\\" + screenName));

}

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.