问题描述
我正在使用 Protractor (v 1.3.1) 为我的 Angular 1.2.26 应用程序运行 E2E 测试.
I'm using Protractor (v 1.3.1) to run E2E tests for my Angular 1.2.26 application.
但有时,测试可以,有时则不行.似乎有时检查是在显示更新之前完成的(或类似同步"问题).我尝试了很多选择:
But sometimes, tests are ok, sometimes not. It seems that sometimes the check is done before display is updated (or something like "synchronisation" problem).I try many options :
- 添加
browser.driver.sleep
指令, - 使用
browser.executeScript('$.fx.off = true')
禁用效果 - 添加
browser.waitForAngular()
说明
- add
browser.driver.sleep
instructions, - disable effects with
browser.executeScript('$.fx.off = true')
- add
browser.waitForAngular()
instructions
没有成功.
使用量角器进行可靠的 E2E 测试的最佳实践是什么?
What are the bests practice to have reliables E2E tests with protractor?
JM.
推荐答案
每次遇到类似问题,我都会使用 browser.wait()
和 "预期条件"(在量角器 1.7 中引入).有一组内置的预期条件通常就足够了,但您可以轻松定义自定义预期条件.
Every time I have similar issues, I'm using browser.wait()
with "Expected Conditions" (introduced in protractor 1.7). There is a set of built-in expected conditions that is usually enough, but you can easily define your custom expected conditions.
例如,等待一个元素变得可见:
var EC = protractor.ExpectedConditions;
var e = element(by.id('xyz'));
browser.wait(EC.visibilityOf(e), 10000);
expect(e.isDisplayed()).toBeTruthy();
一些注意事项:
您可以指定自定义错误消息,以防不满足条件并抛出超时错误,请参阅等待超时错误的自定义消息:
browser.wait(EC.visibilityOf(e), 10000, "Element 'xyz' has not become visible");
您可以将 EC
设置为指向 protractor.ExpectedConditions
的全局可用变量.将此行添加到配置中的 onPrepare()
:
you can set EC
to be a globally available variable pointing to protractor.ExpectedConditions
. Add this line to the onPrepare()
in your config:
onPrepare: function () {
global.EC = protractor.ExpectedConditions;
}
作为自定义预期条件的示例,请参阅此答案
这篇关于如何获得量角器可靠的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!