Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

What is the difference between executeAsyncScript and executeScript? How can I use an event such as window.onload? I tried something like this

((JavascriptExecutor) driver).executeAsyncScript("window.onload = function() {alert('Hello')}"); 

But of course, it did not work. So if anyone knows how it works please write an example

1 Answer

0 votes
by (62.9k points)

Example of executeAsyncScript

In order to improve the performance of your test, we use the executeAsyncScript. This method allows writing test more like a normal writing a code.

The execSync blocks further actions being performed by the Selenium browser but execAsync does not block action. Once the script is done, it will send a callback to the server-side Testing suite. By this, it means everything inside the script will be executed not by the server but by the browser.

Example 1: In the browser under test., performing a sleep 

In this scenario, we will use the "Facebook" demo site to illustrate executeAsyncScript. In this example, you will

Launch the browser.

Open site "https://facebook.com ".

The application waits for 5 sec to perform further action.

Step 1) Capture the start time before waiting for 5 seconds ( 5000 milliseconds) by using the executeAsyncScript() method.

Step 2) Then, use executeAsyncScript() to wait 5 seconds.

Step 3) Then, get the current time.

Step 4) Subtract (current time – start time) = passed time.

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;

    

public class JavaSE_Test {

    @Test

    public void Login()

    {

        

        WebDriver driver= new FirefoxDriver();

        //Creating the JavascriptExecutor interface object by Type casting

        JavascriptExecutor js = (JavascriptExecutor)driver;

        

        //Launching the Site.

        driver.get("https://facebook.com/");

     

          //Maximize window

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

        

          //Set the Script Timeout to 20 seconds

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

             

          //Declare and set the start time

          long start_time = System.currentTimeMillis();

                   

          //Call executeAsyncScript() method to wait for 5 seconds

          js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");

          

         //Get the difference (currentTime - startTime)  of times.

         System.out.println("Passed time: " + (System.currentTimeMillis() - start_time));

                    

    }

}

Example of executeScript

For executeScript, we will see three different example one by one.

1) Example: Click a button to login and generate the Alert window using JavaScriptExecutor.

To illustrate JavaScriptExecutor, we will use the "Facebook" demo site in this scenario. In this example,

Launch the web browser

open the site "https://facebook.com "and

login with credentials

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

    

public class JavaSE_Test {


 

    @Test

    public void Login()

    {

        WebDriver driver= new FirefoxDriver();

        

        //Creating the JavascriptExecutor interface object by Type casting

        JavascriptExecutor js = (JavascriptExecutor)driver;

        

        //Launching the Site.

        driver.get("https://facebook.com/");

        

        WebElement button =driver.findElement(By.name("Log In"));

        

        //Login to Facebook

        driver.findElement(By.name("email")).sendKeys("[email protected]");

        driver.findElement(By.name("password")).sendKeys("yourpassword");

        

        //Perform Click on LOGIN button using JavascriptExecutor

        js.executeScript("arguments[0].click();", button);

                                

        //To generate Alert window using JavascriptExecutor. Display the alert message

        js.executeScript("alert('Welcome to Facebook');");   

    

    }

}

Hope this helps!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...