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) {
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