期望.then内的条件不会执行

时间:2018-04-25 10:23:56

标签: angularjs promise jasmine protractor e2e-testing

我在Protractor测试中使用PageObjects。 结构看起来像这样,

  • E2E
    • 规格
      • base.po.ts // Base PageObject类
      • 登录
        • login.e2e-spec.ts //包含描述块等等和预期条件。
        • login.po.ts //与页面元素交互

我从 PageObject 文件中的方法返回Promises。然后在中的 spec 文件中阻止我期待条件。

示例代码如,

// 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

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

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

2 个答案:

答案 0 :(得分:0)

我还在Protractor github存储库上发布了该问题,并从 IgorSasovets 获得了this解决方案

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

select t1.costs, 
    t1.date,
    (costs * ratio) as ratio
from #Temp1 t1
INNER JOIN #Temp2 t2 ON t2.month = SUBSTRING(t1.date,5,2)

答案 1 :(得分:0)

您需要在验证expect中的值之前解决该承诺 一些

page.getLoginButton()。然后(功能(状态){

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

相关问题