量角器的预期条件 [英] Expected conditions in protractor
问题描述
在用Python编写硒测试时,我习惯于使用显式等待等待页面加载,或等待元素可见或可点击等:
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.
现在,我们将很多端到端测试基础结构切换到protractor
.
Now, we are switching our end-to-end testing infrastructure to protractor
a lot.
量角器中的Expected Conditions
是否与 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
?
我浏览了量角器文档,却一无所获. >
I've looked through protractor documentation and found nothing about it.
推荐答案
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屋!