Last active
July 8, 2019 19:42
-
-
Save compulim/c58c5256bec3ebb4aa23ae228c0844df to your computer and use it in GitHub Desktop.
selenium-webdriver.executePromiseScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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