【发布时间】:2018-10-05 18:54:23
【问题描述】:
我在量角器测试中使用 PageObjects。 结构是这样的,
- e2e
- 规格
- base.po.ts // 基 PageObject 类
- 登录
- login.e2e-spec.ts // 包含 describe、it 块等和 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
实际上我有多个测试,所以控制只是转移到下一个测试,最后告诉所有测试都成功通过。
现在我明白控制是将命令放入队列并继续前进。我该如何处理这种情况?我做错了什么?谢谢。
【问题讨论】:
-
似乎错误来自您的
clickIfElementIsAvailable或waitForVisibility函数。可以发一下吗? -
如果您使用
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