量角器期望元素最终存在 [英] Protractor expectation that element is eventually present
问题描述
有没有办法期望元素最终在页面上?例如一种方式
Is there a way to have an expectation that an element is eventually on the page? e.g. a way for
browser.wait(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))), 1000, 'Unable to find continue link');
因期望错误而非超时而失败?基本上是一种方法,在下面的行中有一个 isEventuallyPresent()
而不是 isPresent()
to fail with an expectation error instead of a timeout? Essentially a way to have a isEventuallyPresent()
instead of isPresent()
in the line below
expect(element(by.partialLinkText('Continue')).isPresent()).toBe(true);
作为参考,我使用 browser.ignoreSynchronization = true
即使它是一个Angular应用程序,并使用Jasmine(至少目前为止)。
For reference, I'm using browser.ignoreSynchronization = true
even though it's an Angular app, and using Jasmine (at least for now).
推荐答案
使用的事实
-
browser.wait
返回一个条件函数返回真值后解析的promise,如果它的次数被拒绝则返回out。
browser.wait
returns a promise that is resolved once the condition function returns truthy, or rejected if it times out.
如果 期望
传递承诺,它只在承诺解决时运行期望
If expect
is passed a promise, it only runs the expectation when the promise is resolved
你可以创建一个包含对 browser.wait的调用的函数
You can make a function that wraps a call to browser.wait
function eventual(expectedCondition) {
return browser.wait(expectedCondition, 2000).then(function() {
return true;
}, function() {
return false;
});
}
然后创建期望
expect(eventual(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))))).toBe(true);
或者,为了使其适用于任何浏览器实例,您可以对Protractor原型进行猴子补丁
Or, to make it work on any browser instance, you can monkey-patch the Protractor prototype
protractor.Protractor.prototype.eventual = function(expectedCondition) {
return this.wait(expectedCondition, 2000).then(function() {
return true;
}, function() {
return false;
});
}
可以用作
expect(browser.eventual(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))))).toBe(true);
为避免超时,您必须确保超时传递到浏览器。等待
小于Jasmine异步测试超时,指定为 jasmineNodeOpts:{defaultTimeoutInterval:timeout_in_millis}
量角器配置文件
To avoid timeouts, you must make sure that the timeout passed to browser.wait
is less than the the Jasmine async test timeout, which is specified as jasmineNodeOpts: {defaultTimeoutInterval: timeout_in_millis}
in the protractor configuration file
这篇关于量角器期望元素最终存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!