implicit and explicit waits in selenium webdriwer, how to

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

Upload: yaroslav-pernerovsky

Post on 22-Jan-2018

823 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Implicit and Explicit waits in Selenium WebDriwer, how to

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

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

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

Problem

Page 5: Implicit and Explicit waits in Selenium WebDriwer, how to

Problem

Page 6: Implicit and Explicit waits in Selenium WebDriwer, how to

Problem

Page 7: Implicit and Explicit waits in Selenium WebDriwer, how to

Problem

Page 8: Implicit and Explicit waits in Selenium WebDriwer, how to

@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

Real Case

Page 10: Implicit and Explicit waits in Selenium WebDriwer, how to

Real Case

Page 11: Implicit and Explicit waits in Selenium WebDriwer, how to

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

WebDriver waits

chromedriver

geckodriver

IEDriverServer

Implicit waits here Explicit waits here

Page 13: Implicit and Explicit waits in Selenium WebDriwer, how to

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

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

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

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

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

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

What's wrong?

wait.until(driver ->

driver.findElement(locator));

Page 20: Implicit and Explicit waits in Selenium WebDriwer, how to

WebDriver Exceptions and RemoteWebDriver

Page 21: Implicit and Explicit waits in Selenium WebDriwer, how to

Solution?

wait.until(driver ->

driver.findElements(locator).size() > 0);

Page 22: Implicit and Explicit waits in Selenium WebDriwer, how to

Mixed waits issues

Page 23: Implicit and Explicit waits in Selenium WebDriwer, how to

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

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

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

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

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

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

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

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

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

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

Questions