Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LDNSE #14 Mark Collin: All in Good Time

LDNSE #14 Mark Collin: All in Good Time

Mark Collin talking about Selenium Wait at London Selenium Meetup

Avatar for London Selenium Meetup

London Selenium Meetup

May 21, 2015
Tweet

More Decks by London Selenium Meetup

Other Decks in Programming

Transcript

  1. Implicit waits are evil! • They slow your checks down!

    • It’s not always obvious that they have been set. • They are left over baggage from Selenium 1
  2. • Remember, when you set an implicit wait it lives

    for the life of the driver object. • Try to make it clear that you are setting implicit waits in your code. But I want to use them OK you can use them, but be careful!
  3. Explicit Waits! ! WebElement payButton = driver.findElement(By.id("pay_now"));
 WebDriverWait wait =

    new WebDriverWait(driver, 15, 500);
 wait.until(ExpectedConditions.visibilityOf(payButton)); There’s a pre-defined ExpectedConditions class in Java
  4. Or maybe the Fluent Wait API (The core of Explicit

    Waits) • Predicates - Return a boolean • Functions - Return anything Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
 .withTimeout(15, TimeUnit.SECONDS)
 .pollingEvery(500, TimeUnit.MILLISECONDS)
 .ignoring(NoSuchElementException.class)
 .ignoring(StaleElementReferenceException.class)
 .withMessage("The message you will see in if a TimeoutException is thrown");
 
 wait.until(<Predicate> or <Function>)
  5. What can I check for with Explicit/Fluent Waits? • Existence

    of elements • Visibility of elements • Invisibility of elements • Elements to stop/start moving • Alerts • Javascript hooks to be fired • Javascript values to be set • Pretty much anything!
  6. public class WebElementLookup {
 private static final Logger LOG =

    LoggerFactory.getLogger(WebElementLookup.class);
 
 public static final int DEFAULT_TIMEOUT_IN_SECONDS = 10;
 private static final int POLL_INTERVAL_IN_MILLISECONDS = 300;
 
 private final By selector;
 private final int timeout;
 
 public WebElementLookup(final By selector, final int timeout) {
 this.selector = selector;
 this.timeout = timeout;
 }
 
 public WebElementLookup(final By selector) {
 this(selector, DEFAULT_TIMEOUT_IN_SECONDS);
 }
 
 public WebElement element(final WebDriver driver) {
 WebDriverWait wait = new WebDriverWait(driver, timeout, POLL_INTERVAL_IN_MILLISECONDS);
 return wait.until(new ExpectedCondition<WebElement>() {
 public WebElement apply(WebDriver driver) {
 LOG.debug("Looking up elements for selector {}", selector);
 final List<WebElement> elements = driver.findElements(selector);
 for (final WebElement webElement : elements) {
 if (matches(webElement)) {
 return webElement;
 }
 }
 return null;
 }
 });
 }
 
 private boolean matches(final WebElement webElement) {
 if (webElement == null) {
 return false;
 }
 return true;
 }
 } public WebElement findElement(final WebElementLookup webElementLookup) {
 return webElementLookup.element(driver);
 } public void clickBackLink() {
 driver.findElement(new WebElementLookup(By.className("back"))).click();
 }
  7. Waiting anti-patterns. • Writing you own wait framework. • Mixing

    waiting strategies. • Waiting for a fixed period of time e.g. Thread.sleep()