How to use JavascriptExecutor in Selenium Webdriver using java

JavaScriptExecutor : JavaScriptExecutor is an interface which helps to execute Javascript through selenium driver. It has two 'executescript' & 'executeAsyncScript' methods to run JavaScript in the context of the currently selected frame or window. Javascript plays very important role when we are not able to perform complex operation on the element on the webpage. We can use all HTML DOM methods and properties when using javascript.
In simple words  “Javascript can be executed within the browser with the help of JavaScript Executor.”

Package:
import org.openqa.selenium.JavascriptExecutor;

Methods
Name: executeAsyncScript(java.lang.String script, java.lang.Object... args)
Description: Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.

Name: executeScript(java.lang.String script, java.lang.Object... args)
Description: Executes JavaScript in the context of the currently selected frame or window.

Syntax:
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript(Script,Arguments);
        script - The JavaScript to execute
        Arguments - The arguments to the script.(Optional)

The list of Scenario’s below you can handle

Alert Pop window
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('JavascriptExecutor');");

Click a button in Selenium WebDriver using JavaScript
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

Refresh browser window using Javascript
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("history.go(0)");

Get innertext of the entire web page in Selenium Webdriver
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
String sText =  js.executeScript("return document.documentElement.innerText;").toString();

Get Web page title
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
String sText =  js.executeScript("return document.title;").toString();

Handle Scroll on Web page
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 50  pixels
js.executeScript("window.scrollBy(0,50)");

Click on a SubMenu which is only visible on mouse hover on Menu
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Automation Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");

Navigate to one page to other page
Code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.location = 'https://www.google.com/'");


Demo Source Code


 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class JavaScriptExecuterExample {  
 public static void main(String[] args) throws InterruptedException {  
 //Create FireFox object   
 WebDriver driver = new FirefoxDriver();  
 //Launching the browser application  
 driver.get("http://hiromia.blogspot.com/");  
 //10 second waittig time  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
 //Maximize window  
 driver.manage().window().maximize();  
 
//Creating the Javascriptexecutor interface object by Type casting  
 JavascriptExecutor js = (JavascriptExecutor) driver;
  
 //Click on Selenium Webdriver level button  
 js.executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*[@id='Label1']/div/ul/li[3]/a")));  

 //Fetching the Domain Name   
 System.out.println("Domain = " + js.executeScript("return document.domain;").toString());  
 //Fetching the URL     
 System.out.println("URL = " + js.executeScript("return document.URL;").toString());  
 //Fetching the Title  
 System.out.println("Title = " + js.executeScript("return document.title;").toString());  
 //refresh browser window  
 js.executeScript("history.go(0)");  
 Thread.sleep(3000);  

 //Vertical scroll - down by 100 pixels  
 js.executeScript("window.scrollBy(0,1000)");  
 // Navigate to google  
 js.executeScript("window.location = 'https://www.google.com/'");  
 Thread.sleep(5000);  

 //close Firefox  
 driver.quit();  
   }  
 }  







1 comment:

  1. How to switch focus to new window using javascriptexecutor

    ReplyDelete