量角器中的预期条件 [英] Expected conditions in protractor
问题描述
在 Python 中编写 selenium 测试时,我习惯于经常使用 Explicit Waits等待页面加载,或等待元素变为可见或可点击等:
While writing selenium tests in Python, I got used to using Explicit Waits a lot for waiting for a page to load, or for waiting for an element to become visible, or clickable etc:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
这里的关键概念是提供一个预期条件来等待,有多种类型:
The key concept here is providing an Expected Condition to wait for, there are multiple types:
与使用具有硬编码时间间隔的 sleep
相比,使用预期条件使代码更清晰、更可靠.
Using Expected Conditions makes the code cleaner and more reliable comparing to using sleep
s with hardcoded time intervals.
现在,我们将端到端的测试基础架构大量切换到量角器
.
Now, we are switching our end-to-end testing infrastructure to protractor
a lot.
量角器中是否有与 python-selenium
或java-selenium
>?如果不是,在 protractor
中显式等待条件的规范方法是什么?
Are there similar Expected Conditions
in protractor as there are in python-selenium
or
java-selenium
? If not, what is the canonical way to explicitly wait for a condition in protractor
?
我浏览了量角器文档,但一无所获.>
推荐答案
Once feat(expectedConditions) 在(可能是量角器 1.7),你可以这样做:
Once feat(expectedConditions) is in (probably protractor 1.7), you can do:
var EC = protractor.ExpectedConditions;
var e = element(by.id('xyz'));
browser.wait(EC.presenceOf(e), 10000);
expect(e.isPresent()).toBeTruthy();
但请注意,如果您正在使用 Angular 应用程序并且您的测试需要这些条件等待,则这是您正在做的事情的一个很大的危险信号,因为量角器应该本地处理等待.
Please note though, if you're working with an Angular app and your test requires these conditional waits, it's a big red flag for what you're doing, as protractor should handle waits natively.
这篇关于量角器中的预期条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!