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!
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>)
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!
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(); }