【问题标题】:Expect condition inside the .then won't execute.then 中的期望条件不会执行
【发布时间】:2018-10-05 18:54:23
【问题描述】:

我在量角器测试中使用 PageObjects。 结构是这样的,

  • e2e
    • 规格
      • base.po.ts // 基 PageObject 类
      • 登录
        • login.e2e-spec.ts // 包含 describeit 块等和 Expect 条件。
        • login.po.ts // 与页面元素交互

我正在从 PageObject 文件中的方法返回 Promise。然后在 it 块中的 spec 文件中,我有 Expect 条件。

示例代码是这样的,

// login.e2e-spec.ts

    it('should logout', () => {
          console.log('---- step 1 ----');
          page.logout().then(function () {
              console.log('---- step 4 ----');
              expect(page.inDom(page.getLoginButton())).toBe(true);
          });
          console.log('---- step 5 ----');
    });



// login.po.ts
public logout() {
        const that = this;
        return new Promise(function (fulfill = null, reject = null) {
            that.clickIfElementIsAvailable(that.welcomeModelCancelButtonElement);
            that.waitForVisibility(that.sideBarOpenerElement).then(function () {
                console.log('---- step 2 ----');
                that.sideBarOpenerElement.click();

                that.waitForVisibility(that.logoutButtonElement).then(function () {
                    console.log('---- step 3 ----');
                    that.logoutButtonElement.click();
                    fulfill();

                }, reject);

            }, reject);
        });
    }

执行后,所有测试都通过,我在日志中得到以下输出。

---- step 1 ----
---- step 5 ----
(node:2639) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'waitForElement' of undefined
(node:2639) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
.


3 specs, 0 failures

实际上我有多个测试,所以控制只是转移到下一个测试,最后告诉所有测试都成功通过。

现在我明白控制是将命令放入队列并继续前进。我该如何处理这种情况?我做错了什么?谢谢。

【问题讨论】:

  • 似乎错误来自您的clickIfElementIsAvailablewaitForVisibility 函数。可以发一下吗?
  • 如果您使用async/await 编写页面对象会更好。 protractortest.org/#/page-objects#with-page-objects
  • @jithinkmatthew:谢谢。我用 async/await 尝试过,但没有任何改变。当我在承诺中承诺时,我对使用等待条件持怀疑态度。例如,如果我只是将 await 与 that.waitForVisibility(that.sideBarOpenerElement) 一起使用,那么 .then 块中的所有代码(包括承诺)就足够了吗?等待 that.waitForVisibility(that.sideBarOpenerElement).then(function () { ....... });
  • 通过在每个承诺上使用 RETURN 语句解决了这个问题。 github.com/angular/protractor/issues/4787

标签: angularjs promise jasmine protractor e2e-testing


【解决方案1】:

你需要先解决promise,然后再验证expect中的值 一些

page.getLoginButton().then(function(status){

期望(状态).toBe(真); })

【讨论】:

    【解决方案2】:

    我还在 Protractor github 存储库上发布了这个问题,并从 IgorSasovets

    获得了this 解决方案

    在每个 Promise 上使用 Return 语句。当前代码如下所示,

    public logout() {
        const that = this;
        return that.clickIfElementIsAvailable(that.welcomeModelCancelButtonElement)
            .then(() => that.waitForVisibility(that.sideBarOpenerElement))
            .then(() => {
                console.log('---- step 2 ----');
                return that.sideBarOpenerElement.click();
            })
            .then(() => that.waitForVisibility(that.logoutButtonElement))
            .then(() => {
                console.log('---- step 3 ----');
                return that.logoutButtonElement.click();
            });
    }
    
    
    
    it('should logout', () => {
          console.log('---- step 1 ----');
          return page.logout().then(() => {
              console.log('---- step 4 ----');
              expect(page.inDom(page.getLoginButton())).toBe(true);
              console.log('---- step 5 ----');
          });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2013-11-29
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多