implicit and explicit waits in selenium webdriwer, how to
Post on 22-Jan-2018
823 Views
Preview:
TRANSCRIPT
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
top related