executeAsync
Overview
Runs JavaScript in the current frame and returns the value passed to a callback. This command is deprecated: prefer execute with an async function for new code.
The command injects the callback as the last argument of the browser function. Do not pass it as a separate argument to browser.executeAsync. Call the callback to complete the command.
Usage
await browser.executeAsync(script, ...args)
Command parameters
script: a function or a string containing a function body. Runs in the browser without Node.js APIs or local closures.args: serializable arguments passed before the callback; WebElement references become DOM elements.
Returns a Promise with the value passed to the callback. In Classic, configure the script timeout using setTimeout. The BiDi implementation has no separate callback timer; always call it and bound the test with testTimeout.
Example
Set baseUrl to a test HTML page.
const assert = require("node:assert/strict");
it("browser.executeAsync: return a result through the injected callback", async ({ browser }) => {
await browser.url("/");
await browser.setTimeout({ script: 5000 });
const result = await browser.executeAsync(
(a, b, done) => {
setTimeout(() => done(a + b), 20);
},
4,
6,
);
assert.equal(result, 10);
});
Related commands
References
This page draws on the original WebdriverIO article.