在Protractor中的async await被忽略

时间:2018-01-18 14:45:35

标签: javascript selenium-webdriver protractor

我是新手测试和JS。我正在尝试编写我的第一个异步等待测试,但我无法使其工作。

我已设置protractor.promise.USE_PROMISE_MANAGER = false;能够使用async await

当我将selenium promise manager设置为false时,我按预期收到错误TypeError: Unable to create a managed promise instance: the promise manager has been disabled by the SELENIUM_PROMISE_MANAGER environment variable: undefined.

但是,当我将async await添加到代码中的所有抽象层时,同样的错误仍然存​​在。如何使用async await进行以下测试?

login.spec.js

protractor.promise.USE_PROMISE_MANAGER = false;

describe('Login page :', () => {
  const loginPage = new objects.Login(),
        el = new objects.Element(),
        ec = new objects.ExpectedCondition()


  beforeAll( async () => {
    await loginPage.go();
  });

  describe('with invalid credentials', () => {

    beforeEach( async () => {
      await browser.refresh();
    }); 

    it('should fail to log in without credentials', async () => {
      await loginPage.submit();

      await expect(ec.isVisible(loginPage.selectors.login.errors.lblEmailValidation));
      await expect(ec.isVisible(loginPage.selectors.login.errors.lblPasswordValidation));
    });

login.po.js

class Login {
  ...
  async submit() {
    await this.element.clickElement(this.selectors.login.button.btnSubmit)
  }
  ...
}

element.js

class Element {
...
  async clickElement(selector) {
    return await this.getElement(selector).click();
  }
...
}

expectedCondition.js

const EC = protractor.ExpectedConditions;

class ExpectedCondition {
  constructor() {
   this.el = new objects.Element();
  }

 async isVisible(selector) {
    return await browser.wait(EC.visibilityOf(this.el.getElement(selector)));
  }
}

element.js

class Element {
  getElement(selector) {
    return element(by.css(selector));
  }
  ...
}

我得到的错误:

Failures:
1) Login page : with invalid credentials should fail to log in without credentials
  Message:
    TypeError: Unable to create a managed promise instance: the promise manager has been disabled by the SELENIUM_PROMISE_MANAGER environment variable: undefined
  Stack:
    TypeError: Unable to create a managed promise instance: the promise manager has been disabled by the SELENIUM_PROMISE_MANAGER environment variable: undefined
        at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1030:13)
        at new Deferred (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1454:20)
        at new Task (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2778:5)
        at ControlFlow.execute (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2477:16)
        at UserContext.<anonymous> (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:19)

1 个答案:

答案 0 :(得分:4)

1)您应该从protractor.promise.USE_PROMISE_MANAGER = false;以及login.spec.js个文件中的所有位置移除*.spec.js;

2)您应该更新protractor.conf文件:

config = {
...
SELENIUM_PROMISE_MANAGER: false,
}