Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WebDriver Waits

451 views

Published on

Selenium Camp 2018

Published in: Internet
  • Be the first to comment

  • Be the first to like this

WebDriver Waits

  1. 1. Selenium Waits deep dive
  2. 2. Yaroslav Pernerovsky Automation Team Lead, GlobalLogic /in/yaroslav-pernerovsky-0559b51 /yaroslav.pernerovsky
  3. 3. @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(); }
  4. 4. @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(); }
  5. 5. Sleep Thread.sleep(1000);
  6. 6. Sleep
  7. 7. Sleep
  8. 8. Sleep //Insanely Long program operation timeInSeconds = 60*50; ... sleep(timeInSeconds*1000);
  9. 9. Sleep
  10. 10. Sleep
  11. 11. Sleep
  12. 12. Sleep
  13. 13. Sleep
  14. 14. while(true) { if (System.currentTimeMillis()-startTime > timeout){ throw new TimeoutException(); } try { return driver.findElement(locator); } catch (NoSuchElementException e) {} Thread.sleep(500); }
  15. 15. while(true) { if (System.currentTimeMillis()-startTime > timeout){ throw new TimeoutException(); } try { return driver.findElement(locator); } catch (NoSuchElementException e) {} Thread.sleep(500); } Timeout Exception Polling interval Exception ignoring Wait condition Timeout exit condition
  16. 16. Implicit wait Explicit wait
  17. 17. chromedriver geckodriver IEDriverServer Implicit waits hereExplicit waits here
  18. 18. Implicit Wait driver.manage().timeouts(). implicitlyWait(10, TimeUnit.SECONDS); driver.manage().timeouts(). pageLoadTimeout(10, TimeUnit.SECONDS); driver.manage().timeouts(). setScriptTimeout(10, TimeUnit.SECONDS);
  19. 19. findElement() • Wait until element appeared in DOM • Return first element if more than one present • Throws NoSuchElementException Implicit Wait
  20. 20. 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
  21. 21. Implicit Wait
  22. 22. Implicit waits are a terrible mistake I've made and I apologies for them Simon Stewart, d Creator of WebDriver
  23. 23. 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 Wait
  24. 24. public boolean isElementNotPresent(By locator) { try{ driver.manage().timeouts() .implicitlyWait(0, TimeUnit.SECONDS); return driver.findElements(locator).size() == 0; } finally{ driver.manage().timeouts() .implicitlyWait(default, TimeUnit.SECONDS); } } Implicit Wait
  25. 25. Implicit Wait
  26. 26. Implicit Wait
  27. 27. Implicit Wait
  28. 28. WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(driver -> driver.findElement(locator)); wait.until(ExpectedConditions .elementToBeClickable(locator)); Explicit Wait
  29. 29. Explicit Wait WebDriverWait wait = new WebDriverWait(driver, 10,100); wait.withMessage("Error Message"); wait.ignoring(Exception.class);
  30. 30. wait.until(driver -> driver.findElement(locator)); Explicit Wait
  31. 31. Explicit Wait
  32. 32. wait.until(driver -> driver.findElements(locator).size() > 0); Explicit Wait
  33. 33. Explicit Wait
  34. 34. 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
  35. 35. 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
  36. 36. 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; } }
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 46 % 29 % 20 % 5 %

×