此代码可能因什么原因失败(未找到元素)...
element(by.id('loginButton')).click(); // triggers route change
browser.wait(element(by.tagName('myComponent')).isPresent(), 10000, 'timeout');
element(by.tagName('myComponent')).click();
...当这段代码有效时?
element(by.id('loginButton')).click(); // triggers route change
const eC = protractor.ExpectedConditions;
browser.wait(eC.visibilityOf(element(by.tagName('myComponent'))), 10000, 'timeout');
element(by.tagName('myComponent')).click();
我正在使用 Angular 5.2.5、Protractor 5.3.0 和 Jasmine 2.8.0。
可能相关:我也可以问为什么我需要添加一个 browser.wait()
而 element(by())
应该由 Protractor 自动添加到 ControlFlow 中,但已经有很多相关问题(here、here、there、there、...),不幸的是没有明确的答案。
最佳答案
两者之间有一个不太明显的区别。但是 webdriver docs对此很清楚。
eC.visibilityOf(...)
- 返回一个函数。 browser.wait() 重复计算函数,直到它们返回 true。
isPresent()
- 返回 promise 。 browser.wait() 不会/不能重复评估 promises(!) browser.wait() 将在 promise 解析时立即继续,无论它返回 true 还是 false。
如果您想使用 isPresent(),您可以将其包装在一个函数中。这允许 webdriver 一遍又一遍地调用它。
browser.wait(() => element(by.tagName('myComponent')).isPresent(), 10000, 'timeout');
完全按照您的预期工作。
关于javascript - Protractor browser.wait 不适用于 Angular 上的元素(by()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276179/