angulartypescriptprotractor

How to avoid using await on every single line


When using Protractor for E2E Testing, it seems as though every single line of code requires await when SELENIUM_PROMISE_MANAGER: false.

Is there a better way to do this example below?

Originally I was using the SELENIUM_PROMISE_MANAGER: false but ran into issues when needing to use conditionals WebElement.isPresent(). The promise manager was not able to resolve the promises of the isPresent() and just continued execution. So now considering using async/await.

describe('Home Page', async () => {

  beforeAll(async () => {
    await loginPage.loadPage();  // methods defined in Page Object
    await loginPage.login();     // uses WebElement.sendKeys() and Webelement.submit()
    await homePage.closeModal();
  });

  it('should fill a form', async () => {
    await $('#field1').clear();
    await $('#field1').sendKeys('hello');
    await $('#field2').clear();
    await $('#field2').sendKeys('world');
    $('form').submit();
  }
}

If almost every line needs an await, is there something i'm missing here?


Solution

  • When using async/await there really is no other way than to put await before calling each function, since you need to make sure everything runs in that particular order, when it comes to E2E testing.
    In your particular case, you could also declare all inside 1 single function.

    //declare this somewhere else:
    this.formFill = async () => {
        await $('#field1').clear();
        await $('#field1').sendKeys('hello');
        await $('#field2').clear();
        await $('#field2').sendKeys('world');
        await $('form').submit();
    };  
    

    Then call it inside the it block:

    it('should fill a form', async () => {
        await this.formFill();
    });