Skip to main content

executeAsync

Overview​

Execute a callback-based browser script with the DOM element as its first argument. The final argument is a callback that sends one result back to the test.

Usage​

element.executeAsync(script, ...args)

Parameters​

ParameterTypeDefaultDescription
scriptFunction | stringRequiredFunction or function body. Call the final callback to finish.
argsunknown[][]Arguments between the DOM element and callback.

Result​

A promise with the value passed to the callback. Returning a value from the script is not a substitute for calling the callback.

Limitations​

Unlike element.execute, this implementation does not explicitly call waitForExist; wait for the element yourself. For Classic sessions, the protocol script timeout is set with browser.setTimeout({ script: milliseconds }). The BiDi path uses script.callFunction and does not implement a separate callback timer: do not rely on the Classic timeout to stop a missing callback there. Always call the callback, and keep a finite Testplane testTimeout.

warning

Deprecated in the bundled WebdriverIO API. For new BiDi tests, prefer element.execute with an async function.

Example​

Set baseUrl to a local HTTP test site where / serves an HTML page. Run the example in a Testplane test file.

const assert = require("node:assert/strict");

it("element.executeAsync: return data through the final callback", async ({ browser }) => {
await browser.url("/");
await browser.execute(() => {
document.body.innerHTML = '<p id="message">Ready</p>';
});
const element = await browser.$("#message");
await element.waitForExist();
await browser.setTimeout({ script: 5000 });
const result = await element.executeAsync((node, prefix, done) => {
setTimeout(() => done(prefix + node.textContent), 20);
}, "Status: ");
assert.equal(result, "Status: Ready");
});

See also​