Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Postman Tutorial Part 28- Building Workflow in Postman Using Collection Runner – New Feature of Postman
Next Post: Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down

Related Posts

REST Assured Tutorial 69 – Response Logging In Rest Assured Uncategorized
image – Make Selenium Easy Uncategorized
Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down | Make Selenium Easy Uncategorized
Make Selenium Easy – Page 4 Uncategorized
FileNameInFileUpload – Make Selenium Easy Uncategorized
Interview Experience at Harman Connected Services Bangalore for Selenium Automation Testing Profile – Oct-2019 Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark