i

Selenium Step By Step Guide

JavascriptExecutor

JavascriptExecutor is an Interface that is used to run javascript through Selenium WebDriver. We see some situations in Selenium where we need to handle some actions which are unable to perform by Selenium WebDriver due to some issues. Such scenarios are like scroll down a web page, clicking on an element, URL name, Domain name, Title of the page, and so on. These types of operations sometimes work very well with the help of javascript, and javascriptExecutor helps us to execute such javascript through Selenium WebDriver.

We need to import “org.openqa.selenium.JavascriptExecutor” to use javascriptExecutor in the automation script. We can see below that javascriptExecutor is an interface, and it extends the WebDriver interface.

javascriptExecutor has two methods “executeScript” and “executeAsyncScript” to execute the javascript on the selected web page. Let us see how to run a javascript by using these methods.

  1. executeAsyncScript:

Asynchronous script means that the script is asynchronously executed at a breakneck speed. This means the script does not force the user to wait for a long time to render the page. This method executes asynchronous javascript in the context of the currently selected window.

We need to explicitly write that the script is finished by invoking the provided callback. The callback must be provided as the last argument into the executed method.

Creating a “wait” using the executeAsyncScript method:

Below are the steps to perform to create a wait for the Selenium script:

1-Check the Start time.

2-Run javaScript to wait for 600 ms.

3-Subtract the Start time from the current time and check if, CurrentTime-StartTime>=6000 ms.

Sample Code:

package SeleniumTest.SeleniumTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class executeAsyncScriptWait

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        JavascriptExecutor execute=(JavascriptExecutor)driver;

        driver.get("http://selflearning.io/");

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

        driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);

        long startTime=System.currentTimeMillis();

        execute.executeAsyncScript("window.setTimeout(arguments[arguments.length-1], 6000);");

        System.out.println("Total pased time: "+ (System.currentTimeMillis()-startTime));

       

 

    }

}

We can see the console output, and it shows 6109 milliseconds, which is higher than 6000 milliseconds as expected.

 

  1. executeScript:

This method executes the javascript in the context of the current window selected. This method uses the document to refer to the current document.

Please note that once the script has finished executing, the local variables will not be available, but the global variables will be available.  Now let us check an example to use executeScript method:

Click on Login button using JavascriptExecutor:

Here we need to follow the below steps to complete this flow:

1-Enter the username.

2-Enter the password.

3-Click on the login button using JavascriptExecutor.

4-Generate an alert pop-up using JavascriptExecutor.

Sample Code:

package SeleniumTest.SeleniumTest;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

 

public class executeScriptClick

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        JavascriptExecutor jscript=(JavascriptExecutor)driver;

        driver.get("http://selflearning.io/login");

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

        driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);

        WebElement eleLoginBtn=driver.findElement(By.xpath("//*[@id='admin_login']/div[3]/div[2]/input"));        driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys("admin@gmail.com");        driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys("admin");

        jscript.executeScript("arguments[0].click();", eleLoginBtn);

        jscript.executeScript("alert('Start Your Self Learning');");       

    }

 We can see below how pop-up gets generated after clicking on login button.

 Note: Here, we have taken dummy username and password, so login is not successful.