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:
text_to_be_present_in_element_value
element_to_be_clickable
alert_is_present
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.
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.
ExpectedConditions View code Represents a library of canned expected conditions that are useful for protractor, especially when dealing with non-angular apps. Each condition returns a function that evaluates to a promise. You may mix multiple conditions using and , or , and/or not .
ExpectedConditions In Selenium WebDriver The Selenium WebDriver waits for the specified condition to occur before it can proceed further with the execution. This provides the required wait time between the actions that have to be performed, e.g. locating the WebElement or other valid operation with the element.
wait(function () { return elem. isDisplayed(); });
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.
In Protractor you can use browser.wait(fn, timeout)
.
Example:
var element = by.id('myDynamicElement');
browser.wait(function() {
return ptor.isElementPresent(element);
}, 10000);
expect(ptor.isElementPresent(element)).toBeTruthy();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With