Part 5: Usages Of Javascripts In Selenium: Understanding Method executeAsyncScript Of JavascriptExecutor

Hello folks,

In last post we have learnt about executeScript methods of JavascriptExecutor interface.

There is another method called executeAsyncScript in JavascriptExecutor interface which is not frequently used. But we must be aware about it usage so that we can use whenever required.

First let’s learn what is Synchronous and Asynchronous?

In very simple words,

Synchronous = Blocking
Asynchronous- Non-blocking

If a program is executed line by line, its called synchronous programming. Until first line finishes execution, second line will not be executed. But if all the lines start execution at same time and it is not blocking any other lines to be executed, is called asynchronous programming. Every asynchronous piece of code signals when execution is done. Signal is called as callback.

You can refer callback in javascript so that it will be easy to understand further.

Example: Java is Synchronous  programming langauge while NodeJS is Asynchronous programming language.

JavaScript is synchronous programming language but we can write some asynchronous commands in javaScript as we can do in Java using Thread concept.

If we want to execute any  asynchronous block of code in Selenium, we need to use executeAsyncScript  method.

Syntax of executeAsyncScript :

java.lang.Object executeAsyncScript(java.lang.String script,java.lang.Object… args)

As per Selenium documentation:

Above methods execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

Suppose, we want to execute below javaScript command:

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

Here, arguments[arguments.length – 1] is a callback function. To get last arguments we need to get the length and minus one. This method calls the function after 5 seconds to execute.

Let’s see above example in code:

Output:

You can see, execution control was waiting till javaScript function is executed completely and signaled. This happened because we are letting JVM know that we are executing async piece of code and wait till function callbacks.

Now, what will happen if we execute async code through executeScript method.

Change the above code as below:

Output:

This time execution control does not wait because JVM does not know about it. This is the reason, to execute asynchronous piece of code, we need to use executeAsyncScript. 

Hope you will have clear understanding of method executeAsyncScript

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning