Selenium Waits
Test automation basics with Selenium & Java
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Waits VS sleeps
● [Link](1000) - stops program execution to 1 second
● WebDriverWait waits for some event
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Selenium waits
● Implicit wait
● Explicit wait
● Fluent wait
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Implicit wait
- waits till element will be displayed on a page
- waits till timeout
- if element was not found, it throws:
[Link]: no such
element
Example:
[Link]().timeouts().implicitlyWait(30,
[Link]); By:
Evgeny Novikov
Email:
xevgnov@[Link]
Explicit wait
1) customizable timeout Example:
2) flexible wait for events: WebDriverWait wait = new
● element's presence WebDriverWait( driver, 30);
[Link](
● element (in-)visibility ExpectedConditions
● till page loaded .elementToBeClickable(searchButton));
● till alert displayed
● till Javascript returns some value
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Do not mix implicit and explicit waits
if you use explicit waits,
then implicit wait should not be used be or set to ‘0’
● implicit wait equals 0 by default,
● it could be set to zero manually:
[Link]().timeouts().implicitlyWait( 0, TimeUnit. SECONDS);
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Custom wait example
boolean isElementDisplayed(By by, int timeout) throws
InterruptedException {
List<WebElement> elements = [Link](by);
for(int i = 0; (i < timeout) && ([Link]() == 0); i++) {
Thread. sleep(1000);
elements = [Link](by);
}
return [Link]() > 0;
}
By:
Evgeny Novikov
Email:
xevgnov@[Link]
Create test with following steps:
- Open [Link]
- wait until page header will be
visible
- wait until search button will be
clickable
Exercise - type some string in search field
- press search button
- verify that search page header
Add to test explicit wait, until is invisible on results page
chosen element would not be Hint:
visible. use
[Link](el
ement)); By:
Evgeny Novikov
Email:
xevgnov@[Link]