![Page 1: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/1.jpg)
YOU ARE DOING IT WRONG ALL THE TIME
The Great Battle of Selenium Waits
![Page 2: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/2.jpg)
Speaker Info
Yaroslav Pernerovsky, Ukraine
Automation Team Lead,
GlobalLogic
/in/yaroslav-pernerovsky-0559b51
/yaroslav.pernerovsky
![Page 3: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/3.jpg)
Problem
Implicit waits are a terrible mistake I've made and I apologies for them
Simon Stewart, d Creator of WebDriver
![Page 4: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/4.jpg)
Problem
![Page 5: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/5.jpg)
Problem
![Page 6: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/6.jpg)
Problem
![Page 7: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/7.jpg)
Problem
![Page 8: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/8.jpg)
@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
![Page 9: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/9.jpg)
Real Case
![Page 10: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/10.jpg)
Real Case
![Page 11: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/11.jpg)
Custom Wait
while(true) {
if (System.currentTimeMillis()-startTime > timeout){
throw new TimeoutException();
}
try {
return driver.findElement(locator);
}
catch (NoSuchElementException e) {}
Thread.sleep(500);
}
![Page 12: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/12.jpg)
WebDriver waits
chromedriver
geckodriver
IEDriverServer
Implicit waits here Explicit waits here
![Page 13: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/13.jpg)
Implicit Waits
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
![Page 14: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/14.jpg)
Implicit Waits
driver.findElement()
• Wait until element appeared in DOM
• Return first element if more than one present
• Throws NoSuchElementException
![Page 15: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/15.jpg)
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
![Page 16: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/16.jpg)
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();
![Page 17: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/17.jpg)
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;
}
![Page 18: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/18.jpg)
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
![Page 19: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/19.jpg)
What's wrong?
wait.until(driver ->
driver.findElement(locator));
![Page 20: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/20.jpg)
WebDriver Exceptions and RemoteWebDriver
![Page 21: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/21.jpg)
Solution?
wait.until(driver ->
driver.findElements(locator).size() > 0);
![Page 22: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/22.jpg)
Mixed waits issues
![Page 23: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/23.jpg)
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
![Page 24: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/24.jpg)
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;
}
}
![Page 25: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/25.jpg)
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
![Page 26: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/26.jpg)
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
![Page 27: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/27.jpg)
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
![Page 28: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/28.jpg)
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
![Page 29: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/29.jpg)
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
![Page 30: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/30.jpg)
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
![Page 31: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/31.jpg)
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
![Page 32: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/32.jpg)
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
![Page 33: Implicit and Explicit waits in Selenium WebDriwer, how to](https://reader031.vdocument.in/reader031/viewer/2022022415/5a65203e7f8b9aa2548b75e1/html5/thumbnails/33.jpg)
Questions