How do I use async and await in protractor tests?
it('test async', function(){
var value = 0;
function asyncAction() {
return browser.driver.wait(()=>true)
.then(function () {
console.log('a');
return value++;
});
}
//-Problem Area-
async function doAwait(){
await asyncAction();
return asyncAction();
}
doAwait();
protractor.promise.controlFlow().execute( () => {
console.log('b');
expect(value).toBe(2);
});
});
output here is
and value is 1 at time of expect function doAwait(){ await asyncAction(); return asyncAction(); }
I like to think of this as similar to
function doAwait(){
asyncAction().then(()=>asyncAction());
}
Which works but the above async doAwait does not. I believe this is because the generator breaks the ControlFlow of selenium.
Adding this to the protractor configuration works:
var webdriver = require.main.require('selenium-webdriver');
Promise = webdriver.promise.Promise;
Object.assign(Promise, webdriver.promise);
Promise.resolve = Promise.fulfilled;
Promise.reject = Promise.rejected;
Though maybe not all promises are supposed to be managed promises?
Worth noting that the other solution requires wrapping each async function:
protractor.promise.controlFlow().execute( async () => {
await asyncAction();
return asyncAction();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With