Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

python - Expected conditions in protractor

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:

Using Expected Conditions makes the code cleaner and more reliable comparing to using sleeps with hardcoded time intervals.

Now, we are switching our end-to-end testing infrastructure to protractor a lot.

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...