/** * Repeatedly applies this instance's input value to the given predicate until the timeout expires * or the predicate evaluates to true. * * @param isTrue The predicate to wait on. */ public void until(final Predicate<T> isTrue) { until( new Function<T, Boolean>() { public Boolean apply(T input) { return isTrue.apply(input); } }); }
/** * Waits until element contains the specified text. Useful for waiting for an AJAX call to * complete. * * @param element the element to observe * @param text when the element contains this text the execution of the program will be continued * @throws TimeoutException */ public void elementText(HtmlElement element, String text) throws TimeoutException { FluentWait<WebDriver> wait = new FluentWait<WebDriver>(browser.getWebDriver()) .withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(500, TimeUnit.MILLISECONDS); // start waiting for given text of element wait.until(new ElementHasTextWaitCondition(element, text)); }
@Test public void waitTest() { // Explicit wait defined using fluent wait FluentWait<WebDriver> fwait = new FluentWait<WebDriver>(driver) .withTimeout(90, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); fwait.until( ExpectedConditions.invisibilityOfElementLocated( By.xpath("html/body/div[3]/div[2]/div/div[1]/div[2]/a"))); fwait .until( ExpectedConditions.visibilityOfElementLocated( By.xpath("html/body/div[3]/div[2]/div/div[1]/div[4]/a"))) .click(); }
// This is an ugly test not using page framework, it has the same function as the test below. :( @Test public void changeLocationUsingExplicitWaitLambda() { WebDriver driver = new ChromeDriver(); driver.get("http://www.ticketfly.com"); driver.findElement(linkText("change location")).click(); WebDriverWait webDriverWait = new WebDriverWait(driver, 5); WebElement location = webDriverWait.until( new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { return driver.findElement(By.id("location")); } }); FluentWait<WebElement> webElementWait = new FluentWait<WebElement>(location) .withTimeout(30, SECONDS) .ignoring(NoSuchElementException.class); WebElement canada = webElementWait.until( new Function<WebElement, WebElement>() { @Override public WebElement apply(WebElement element) { return location.findElement(linkText("CANADA")); } }); canada.click(); WebElement allCanada = webElementWait.until( new Function<WebElement, WebElement>() { @Override public WebElement apply(WebElement element) { return location.findElement(linkText("Ontario")); } }); allCanada.click(); assertEquals(0, driver.findElements(linkText("Ontario")).size()); assertEquals( "Ontario", driver.findElement(By.xpath("//div[@class='tools']/descendant::strong")).getText()); }
/** * Waits until element is found. Useful for waiting for an AJAX call to complete. * * @param query the element query * @throws TimeoutException */ public void query(HtmlQuery query) throws TimeoutException { // WebDriverWait wait = new WebDriverWait(browser.getWebDriver(), timeout); FluentWait<WebDriver> wait = new FluentWait<WebDriver>(browser.getWebDriver()) .withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS); // start waiting for given element wait.until(new ElementWaitCondition(query)); }
protected void waitFor(By by, long poll, long timeout) { FluentWait<By> fluentWait = new FluentWait<By>(by); fluentWait.pollingEvery(poll, TimeUnit.MILLISECONDS); fluentWait.withTimeout(timeout, TimeUnit.SECONDS); fluentWait.until( new Predicate<By>() { public boolean apply(By by) { try { List<WebElement> results = rootElement.findElements(by); return !results.isEmpty() && results.get(0).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); }
/** * Clicks the button located at the given table at the given row and column. It additionally * expects it to be secured with a ConfirmDiaĺog * (https://vaadin.com/directory#addon/confirmdialog). */ public void clickTableButtonWithConfirmation(String tableName, int row, int col) { clickTableButton(tableName, row, col); ConfirmDialogPO popUpWindowPO = new ConfirmDialogPO(driver); popUpWindowPO.clickOKButton(); FluentWait<WebDriver> wait = new WebDriverWait(driver, WaitConditions.LONG_WAIT_SEC, WaitConditions.SHORT_SLEEP_MS) .ignoring(StaleElementReferenceException.class); wait.until( new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { List<WebElement> findElements = driver.findElements(By.xpath("//div[contains(@class, 'v-window ')]")); return findElements.size() == 0; } }); WaitConditions.waitForShortTime(); }
/** * Repeatedly applies this instance's input value to the given function until one of the following * occurs: the function returns neither null nor false, the function throws an unignored * exception, the timeout expires * * <p>Useful in situations where FluentAdapter#await is too specific (for example to check against * page source) * * @param wait generic FluentWait<WebDriver> instance * @param f function to execute */ public <T> T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }