JavaScriptExecutor in Selenium WebDriver With Example
JavaScriptExecutor in Selenium WebDriver With Example
In case, these locators do not work you can use JavaScriptExecutor. You can use
JavaScriptExecutor to perform an desired operation on a web element.
Selenium supports javaScriptExecutor. There is no need for an extra plugin or add-on. You
just need to import (org.openqa.selenium.JavascriptExecutor) in the script as to use
JavaScriptExecutor.
JavaScriptExecutor Methods
1. executeAsyncScript
With Asynchronous script, your page renders more quickly. Instead of forcing users to wait
for a script to download before the page renders. This function will execute an asynchronous
piece of JavaScript in the context of the currently selected frame or window in Selenium. The
JS so executed is single-threaded with a various callback function which runs synchronously.
2. executeScript
This method executes JavaScript in the context of the currently selected frame or window in
Selenium. The script used in this method runs in the body of an anonymous function (a
function without a name). We can also pass complicated arguments to it.
Boolean
Long
String
List
WebElement.
Syntax:
Example of executeAsyncScript
Using the executeAsyncScript, helps to improve the performance of your test. It allows
writing test more like a normal coding.
The execSync blocks further actions being performed by the Selenium browser but
execAsync does not block action. It will send a callback to the server-side Testing suite once
the script is done. It means everything inside the script will be executed by the browser and
not the server.
Step 1) Capture the start time before waiting for 5 seconds ( 5000 milliseconds) by using
executeAsyncScript() method.
Step 2) Then, use executeAsyncScript() to wait 5 seconds.
Step 5) Verify the output it should display more than 5000 milliseconds
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
@Test
public void Login()
{
//Maximize window
driver.manage().window().maximize();
}
}
[TestNG] Running:
C:\Users\gauravn\AppData\Local\Temp\testng-eclipse-387352559\testng-
customsuite.xml
log4j:WARN No appenders could be found for logger
(org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more
info.
Passed time: 5022
PASSED: Login
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
Example of executeScript
For executeScript, we will see three different example one by one.
@Test
public void Login()
{
WebDriver driver= new FirefoxDriver();
//Login to Guru99
driver.findElement(By.name("uid")).sendKeys("mngr34926");
driver.findElement(By.name("password")).sendKeys("amUpenu");
}
}
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
@Test
public void Login()
{
WebDriver driver= new FirefoxDriver();
//Fetching the Domain Name of the site. Tostring() change object to name.
String DomainName = js.executeScript("return
document.domain;").toString();
System.out.println("Domain name of the site = "+DomainName);
//Method document.title fetch the Title name of the site. Tostring() change
object to name
String TitleName = js.executeScript("return document.title;").toString();
System.out.println("Title of the page = "+TitleName);
//Navigate to new Page i.e to generate access page. (launch new url)
js.executeScript("window.location = 'http://demo.guru99.com/'");
}
}
Output: When above code is executed successfully, it will fetch the details of the site and
navigate to different page as shown below.
[TestNG] Running:
C:\Users\gauravn\AppData\Local\Temp\testng-eclipse-467151014\testng-
customsuite.xml
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
@Test
public void Login()
{
WebDriver driver= new FirefoxDriver();
//Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//Maximize window
driver.manage().window().maximize();
Output: When above code is executed, it will scroll down by 600 pixels (see image below).