Protractor by default uses WebDriver JS control flow to resolve promises and helps in synchronising the execution of scripts in correct order.
- Minimum requirement to support async / await
Nodejs >= 8.0
Jasmine >= 2.7
- Need to use async keyword prior to asynchronous functions
- Need to use await keyword in all user actions, so each line will run one by one in order
- in conf.ts file, we need to turn OFF the driver control flow SELENIUM_PROMISE_MANAGER : false;
- By default this is true
in this session, we will see promise error, old way of handling the promises and the new way one by one for better understanding
With out resolving promise
import {browser, element, by} from 'protractor'; describe("Calculator test", function(){ var Header = element(by.tagName('h3')); beforeEach(function(){ browser.get("https://juliemr.github.io/protractor-demo/"); }) it("Launch url check", function(){ let HeaderText = Header.getText(); console.log("Browser Header :-" + HeaderText); }) })
OutPut –
Browser Header :-[object Object]
In the above, as we didn’t handle the promises, that is why we got error and did not get expected output.
Now let’s see the WebDriver control flow way of handling promises
import {browser, element, by} from 'protractor'; describe("Calculator test", function(){ var Header = element(by.tagName('h3')); beforeEach(function(){ browser.get("https://juliemr.github.io/protractor-demo/"); }) it("Launch url check", function(){ let HeaderText = Header.getText(); HeaderText.then(function(txt){ console.log("Browser Header :-" + txt); }); }) })
OutPut –
Browser Header :-Super Calculator
Now let’s see the async / await way of handling promise
import {browser, element, by} from 'protractor'; describe("Calculator test", function(){ var Header = element(by.tagName('h3')); beforeEach(function(){ browser.get("https://juliemr.github.io/protractor-demo/"); }) it("Launch url check", async function(){ HeaderText = await Header.getText(); console.log("Browser Header :-" + HeaderText); }) })
OutPut –
Browser Header :-Super Calculator
async / await reduces the lines of code and also increases the speed of execution.
To learn more about how to implement the async / await with page object model framework, refer below explanation
Reference – https://github.com/angular/protractor/blob/master/docs/async-await.md