11/25/17

JavaScriptExecutor in Selenium WebDriver




JavaScriptExecutor in Selenium WebDriver:
In general, we do click on an element using click() method in Selenium.

Sometimes web controls don’t react well against selenium commands and we may face issues with the above statement (click()). To overcome such kind of situation, we use JavaScriptExecutor interface.


It provides mechanism to execute Javascript through Selenium driver. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.

There is no need to write separate script to execute JavaScript within the browser using Selenium WebDriver script. Just use predefined interface named ‘Java Script Executor’. We need to import the below package in the script.

Package:

import org.openqa.selenium.JavascriptExecutor;


Library File(JavaScriptUtil )

package javaScriptExecutor;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class JavaScriptUtil {


public static void flash(WebElement element, WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
String bgcolor = element.getCssValue("backgroundColor");
for (int i = 0; i < 500; i++) {
changeColor("#000000", element, driver);// 1
changeColor(bgcolor, element, driver);// 2
}
}

public static void changeColor(String color, WebElement element, WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", element);

try {

Thread.sleep(20);
} catch (InterruptedException e) {
}
}

public static void drawBorder(WebElement element, WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].style.border='3px solid red'", element);
}

public static String getTitleByJS(WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
String title = js.executeScript("return document.title;").toString();
return title;
}

public static void clickElementByJS(WebElement element, WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].click();", element);

}


public static void generateAlert(WebDriver driver, String message) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("alert('" + message + "')");

}


public static void refreshBrowserByJS(WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("history.go(0)");
}

public static String getPageInnerText(WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
String pageText = js.executeScript("return document.documentElement.innerText;").toString();
return pageText;
}

public static void scrollPageDown(WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
}

public static void scrollIntoView(WebElement element, WebDriver driver) {

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].scrollIntoView(true);", element);
}

}


Test Case using JavaScriptUtil.java library file

package javaScriptExecutor;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
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;

public class JavascriptExecutorDemo {

public static void main(String[] args) throws IOException {

System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.twoplugs.com/");

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

// Syntax
// -------------
// JavascriptExecutor js = (JavascriptExecutor)driver;
// js.executeScript(script, args);

// flash
// ---------------------
WebElement joinfree = driver.findElement(By.xpath("/html/body/div/header/div/ul/li[2]/a"));
// JavaScriptUtil.flash(joinfree,driver);

// Drawing border & take screenshot
// ------------------
JavaScriptUtil.drawBorder(joinfree, driver);

File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("C://screenshot/twoplugs.png"));

// Getting title of the page
// ---------------------------
String title = JavaScriptUtil.getTitleByJS(driver);
System.out.println(title);

// Clicking on element
// --------------------------
// WebElement
// loginBtn=driver.findElement(By.xpath("/html/body/div/header/div/ul/li[1]/a/span"));
// JavaScriptUtil.clickElementByJS(loginBtn,driver);

// Generate alert
// --------------
// JavaScriptUtil.generateAlert(driver, "Clicked on Login Button");

// Refreshing page
// ----------------
JavaScriptUtil.refreshBrowserByJS(driver);

// get page inner text
// -------------
// System.out.println(JavaScriptUtil.getPageInnerText(driver));

// Scrolling down page
// JavaScriptUtil.scrollPageDown(driver);

// Scroll we find element on page
WebElement image = driver.findElement(By.id("rslides3_s0"));
JavaScriptUtil.scrollIntoView(image, driver);

}

}


0 comments:

Post a Comment