Skip to content

Instantly share code, notes, and snippets.

@compulim
Last active July 8, 2019 19:42
Show Gist options
  • Save compulim/c58c5256bec3ebb4aa23ae228c0844df to your computer and use it in GitHub Desktop.
Save compulim/c58c5256bec3ebb4aa23ae228c0844df to your computer and use it in GitHub Desktop.
selenium-webdriver.executePromiseScript
// executeAsyncScript is not running a Promise function and is not able to deal with errors.
// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html#executeAsyncScript
// This function will use executeAsyncScript to run a Promise function in an async fashion.
export default async function executePromiseScript(driver, fn, ...args) {
const { error, result } = await driver.executeAsyncScript((fn, args, callback) => {
eval(`(${fn})`).apply(null, args).then(result => callback({ result }), error => callback({ error }));
}, fn + '', args);
if (error) {
const err = new Error(error.message);
err.stack = error.stack;
throw err;
} else {
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment