executeAsyncScript (JS code, variable) - Selenium IDE command

executeAsyncScript

The executeAsyncScript command executes an async snippet of JavaScript in the context of the currently selected frame or window. The script fragment will be executed as the body of an anonymous function. To store the return value, use the 'return' keyword and provide a variable name in the value input field.

Update: Ui.Vision RPA V7.0 and higher no longer support executeAsyncScript. The reason for this change is the upgrade to the new Chrome manifest V3.

What is asynchronous code?

In asynchronous programs, you can have two lines of code (L1 followed by L2), where L1 schedules some task to be run in the future, but L2 runs before that task completes.

Note that asynchronous does not mean the same thing as concurrent or multi-threaded. JavaScript can have asynchronous code, but it is generally single-threaded. This is like a restaurant with a single worker who does all of the waiting and cooking. But if this worker works quickly enough and can switch between tasks efficiently enough, then the restaurant seemingly has multiple workers.

What is executeAsyncScript good for?

Honestly, we don't know. While asynchronous code in Javascript in general has plenty good uses, we have yet to find any good use case within any Selenium IDE. The executeScript command is an essential Selenium IDE command. But the async version adds no additional benefits in our opinion. If you use execute Async Script in a Selenium IDE project, please let us know how you use it. Maybe we overlook something here. (We offer a free Ui.Vision RPA PRO license for the first real life use case report).

Important: Despite the "async" in the name of the command, the Selenium IDE waits for the executeAsyncScript Javascript code to be completed before the IDE moves to the next command. So in the context of the Selenium IDE the executeAsyncScript is still synchronous - that is what makes it useless.

We added it because it was easy to add and the original Selenium IDE (SIDE) has it as well. But because we see no use case for it, it is not listed in the command drop down. But the command is fully supported and works. If you want to use it, just add in the source code view tab.

executeAsyncScript_Sandbox

The executeAsyncScript_Sandbox works exactly as executeAsyncScript but runs the Javascript in a sandbox. In other words, the code runs not in the website. As with executeScript_Sandbox the advantage of using the sandbox is that the website can not influence or block the Javascript execution. So unless you want to access elements of the website, better use the sandbox.

executeAsyncScript Example

The setTimeout function is probably the simplest way to asynchronously schedule code to run in the future. What happens here is that executeAsyncScript waits for the timeout to happen in 500ms, and then it continues.

Command Target Pattern/Text
executeAsyncScript_Sandbox return new Promise((r) => {setTimeout(() => {r("hello world")}, 500)}) myvar
echo ${myvar}

Works in

Ui.Vision RPA for Chrome Selenium IDE, Ui.Vision RPA for Firefox Selenium IDE, Firefox IDE Classic

Related Demo Macros

DemoStoreEval

The ready-to-import-and-run source code of all demo macros can be found in the Open-Source RPA software Github repository.

See also

executeScript, Web Automation Extension User Manual, Selenium IDE commands, Classic Firefox Selenium IDE.

Anything wrong or missing on this page? Suggestions?

...then please contact us.

<em>Ui.Vision RPA</em> Selenium IDE for Chrome and Firefox - Web Test Automation

Fresh from the Ui.Vision Forum: The Latest 3 Topics. Join the discussions:

← Meet the Ui.Vision team and users in our forums.