22. findElement()
• Wait until element appeared in DOM
• Return first element if more than one present
• Throws NoSuchElementException
Implicit Wait
23. findElements()
• Wait until at least one element appeared in DOM
• Return collection of all found elements
• Return empty collection if no elements found
Implicit Wait
40. 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
41.
42.
43.
44.
45. wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
How long will Test wait ?
5 15
C: 10 25
46. 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;
}
}
47. 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
48. wait = new WebDriverWait(driver,10);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
How long will Test wait ?
5 15
C: 10 25
49. wait = new WebDriverWait(driver,11);
driver.manage().timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
How long will Test wait ?
5 B: 15
11 26
50. wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(locator));
How long will Test wait if element is present?
A: 5 15
10 25
51. wait = new WebDriverWait(driver,5);
driver.manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(locator));
How long will Test wait if element is not present?
C:
5 15
10 25
52.
53. Don't use Implicit wait!
Always set implicit timeout lower than explicit
Timeouts must be multiple to each other
Take special care to 'not present' conditions
Use Selenide