SDET- QA Automation Techie

Full Stack QA Automation Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

JavaScriptExecutor in Selenium WebDriver

 JavaScriptExecutor in Selenium WebDriver With Examples   




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);

}

}


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates