Using async and waiting in selenium tests

How to use async and wait 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); }); }); 

conclusion here

  • a
  • b
  • a

and the value is 1 while waiting doAwait () function {waiting asyncAction (); return asyncAction (); }

I like to think it looks like

 function doAwait(){ asyncAction().then(()=>asyncAction()); } 

Which works, but above, async doAwait does not. I believe this is because the generator destroys the ControlFlow of selenium.

+5
source share
2 answers

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; 

Although perhaps not all promises should be driven by promises?

It is worth noting that another solution requires wrapping each asynchronous function:

 protractor.promise.controlFlow().execute( async () => { await asyncAction(); return asyncAction(); }); 
+4
source

See https://github.com/angular/jasminewd#async-functions--await :

async functions / await

async and the await keyword are most likely included in ES2017 (ES8) and are available through several compilers. Currently, they often disrupt the WebDriver control flow. ( GitHub issue ). You can use them anyway, but if you do, you will have to use await / Promises for almost all of your synchronization. See spec / asyncAwaitAdapterSpec.ts and spec / asyncAwaitErrorSpec.ts for examples.

0
source

All Articles