async/await

Background

How to use native async/await in test

We have a simple example to show how to use async/await in test.

You can find the whole example in here

describe('angularjs homepage', function() {
  it('should greet the named user', async function() {
    await browser.get('http://www.angularjs.org');

    await element(by.model('yourName')).sendKeys('Julie');

    var greeting = element(by.binding('yourName'));

    expect(await greeting.getText()).toEqual('Hello Julie!');
  });

As you can see, the syntax is almost the same with TypeScript async/await.

  1. We need wrap our asynchronous function with “async”.
  2. We can add “await” keyword to each operation that we want our program to wait for.

    Note: Never forget to add “await” keyword in an async function, it may bring some unexpected problem (e.g. your test might fail silently and always be reported as passed).

  3. Don’t forget to turn off control_flow, you cannot use a mix of async/await and the control flow: async/await causes the control flow to become unreliable (see github issue). So if you async/await anywhere in a spec, you should use the SELENIUM_PROMISE_MANAGER: false
// An example configuration file for debugging test using async/await.
exports.config = {
  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  seleniumAddress: 'http://localhost:4444/wd/hub',

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

  // Spec patterns are relative to the current working directory when
  // protractor is called.
  specs: ['async_await.js'],

  SELENIUM_PROMISE_MANAGER: false,

  // Options to be passed to Jasmine.
  jasmineNodeOpts: {
    defaultTimeoutInterval: 30000
  }
};