Implicit and Explicit waits in Selenium WebDriwer, how to.

ysparrow 1,851 views 33 slides Jun 02, 2017
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

How to use waits in Selenium WebDriver


Slide Content

YOU ARE DOING IT WRONG
ALL THE TIME
The Great Battle of Selenium Waits

Speaker Info
Yaroslav Pernerovsky,
Ukraine

Automation Team Lead,
GlobalLogic

/in/yaroslav-pernerovsky-0559b51
/yaroslav.pernerovsky

Problem
Implicit waits are a terrible mistake I've made
and I apologies for them
Simon Stewart, d
Creator of WebDriver

Problem

Problem

Problem

Problem

@Test
public void simpleAction() {
driver.get("http://www.google.com" );
driver.findElement(By.name("q"))
.sendKeys("Search String" + Keys.ENTER);
driver.findElement(By.cssSelector("h3.r"))
.click();
}
Real Case

Real Case

Real Case

Custom Wait
while(true) {
if (System.currentTimeMillis()-startTime > timeout){
throw new TimeoutException();
}
try {
return driver.findElement(locator);
}
catch (NoSuchElementException e) {}
Thread.sleep(500);
}

WebDriver waits
chromedriver
geckodriver
IEDriverServer
Implicit waits here Explicit waits here

Implicit Waits
driver.manage().timeouts(). implicitlyWait(10, TimeUnit.SECONDS);

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts(). setScriptTimeout(10, TimeUnit.SECONDS);

Implicit Waits
driver.findElement()
•Wait until element appeared in DOM
•Return first element if more than one present
•Throws NoSuchElementException

Implicit Waits
driver.findElements()
•Wait until at least one element appeared in DOM
•Return collection of all found elements
•Return empty collection if no elements found

Implicit Waits Common Issue
public boolean isElementPresent(By locator) {
return driver.findElements(locator).size() > 0;
}
if (isElementPresent(login))
driver.findElement(login).click();
if (!isElementPresent(login))
driver.findElement(logout).click();

Implicit Waits Common Issue
public boolean isElementNotPresent(By locator) {

driver.manage().timeouts()
.implicitlyWait(0, TimeUnit.SECONDS);

boolean result= driver.findElements(locator).size() > 0;

driver.manage().timeouts()
.implicitlyWait(defaultTimeOut, TimeUnit.SECONDS);

return result;
}

Explicit Waits
WebDriverWait wait = new
WebDriverWait(driver, 10);

wait.until(driver ->
driver.findElement(locator));

wait.until(ExpectedConditions
.presenceOfElementLocated (locator));

http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

What's wrong?
wait.until(driver ->
driver.findElement(locator));

WebDriver Exceptions and RemoteWebDriver

Solution?
wait.until(driver ->
driver.findElements( locator).size() > 0);

Mixed waits issues

What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);

wait.until(ExpectedConditions
.presenceOfElementLocated (locator);
How long will Selenium wait ?
5 15
C: 10 25

Why?
public static ExpectedCondition<WebElement> presenceOfElementLocated( final By locator) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return findElement(locator, driver);
}
...
private static WebElement findElement(By by, WebDriver driver) {
try {
return driver.findElements(by).stream().findFirst().orElseThrow(
() -> new NoSuchElementException( "Cannot locate an element using " + by));
} catch (NoSuchElementException e) {
throw e;
} catch (WebDriverException e) {
log.log(Level.WARNING,
String.format("WebDriverException thrown by findElement(%s)" , by), e);
throw e;
}
}

What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);

wait.until(ExpectedConditions
.presenceOfElementLocated (locator);
Which exception will be thrown ?
A: Timeout NoSuchElement
Both None

What if?
wait = new WebDriverWait(driver,10);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);

wait.until(ExpectedConditions
.presenceOfElementLocated (locator);
How long will Selenium wait ?
5 15
C: 10 25

What if?
wait = new WebDriverWait(driver,11);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);

wait.until(ExpectedConditions
.presenceOfElementLocated (locator);
How long will Selenium wait ?
5
B: 15
11 26

What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);

wait.until(ExpectedConditions .not(
ExpectedConditions.presenceOfElementLocated (locator));
How long will Selenium wait if element is present?
A: 5 15
10 25

What if?
wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);

wait.until(ExpectedConditions .not(
ExpectedConditions.presenceOfElementLocated (locator));
How long will Selenium wait if element is not present?
C:
5 15
10 25

Coexistence rules
Use Implicit wait for element presence
Do not use Explicit wait for element presence
Always set implicit timeout lower than explicit
Timeouts must be multiple to each other
Take special care to 'not present' conditions

Side-by-Side comparison
Client Side (500ms)
Can wait for anything
Explicit usage
TimeoutException
Multiple network calls
Explicit Implicit
Driver Side (100ms)
Element appeared in DOM
Works automatically
NoSuchElementException
Single network call

Few words about Selenide
WebDriver Implicit Wait not used at all
Custom implicit waits on client side
Default polling interval - 100ms
Remote WebDriver traffic issues

Questions