private <T> T waitFor(Supplier<T> supplier) { WaitingFunction<T> function = new WaitingFunction<>(); try { FluentWait<Supplier<T>> wait = new FluentWait<>(supplier) .ignoring(NoSuchElementException.class); wait.withTimeout(duration); return wait.until(function); } catch (TimeoutException e) { if (function.foundStaleElementReferenceException != null) { throw StaleElementReferenceException .class.cast(function.foundStaleElementReferenceException); } throw e; } }
/** * Wait until {@literal condition} is {@code true} for {@code element}, up to the {@literal timeout} specified * by {@literal unit}. * * @param element the element to examine * @param condition condition that must evaluate to {@literal true}, expressed by a {@link Predicate} * @param timeout timeout to wait for {@literal condition} to come true * @param unit unit of the {@literal timeout} * @throws TimeoutException if the condition does not come true before the timeout expires * @since 2.3 */ public void waitUntil(@Nonnull WebElement element, @Nonnull Predicate<WebElement> condition, long timeout, TimeUnit unit) { new FluentWait<WebElement>(checkNotNull(element, "element")).withTimeout(timeout, unit).until(condition::apply); }
/** * 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 * * Useful in situations where FluentAdapter#await is too specific * (for example to check against page source) * * @param wait generic {@code FluentWait<WebDriver>} instance * @param f function to execute */ public <T>T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }
/** * Create a new {@link Wait} with timeout. * * @param timeout timeout {@link Duration} for the {@link Wait} * @return a new {@link Wait} for the thread local driver and given timeout * which also ignores {@link NoSuchElementException} and * {@link StaleElementReferenceException} */ public static Wait<WebDriver> newWaitWithTimeout(Duration timeout) { return new FluentWait<>(getWebDriver()) .withTimeout(timeout) .ignoring(NoSuchElementException.class) .ignoring(StaleElementReferenceException.class); }
public void clickWhenReady() { FluentWait<FluentElement> wait = new FluentWait<>(this); wait.ignoring(WebDriverException.class); wait.until(new java.util.function.Function<FluentElement, Boolean>() { @Override public Boolean apply(FluentElement input) { input.click(); return true; } }); }
public FluentWait<WebDriver> doWait() { return new FluentWait(driver) .withTimeout(waitForTimeout) .pollingEvery(Duration.ofMillis(WAIT_FOR_ELEMENT_PAUSE_LENGTH)) .ignoreAll(NewList.of(NoSuchElementException.class, NoSuchFrameException.class, StaleElementReferenceException.class, InvalidElementStateException.class)); }
@Test(expected = TimeoutException.class) public void nullPointerExceptionSafetyTestWithPrecondition() { Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context")) .withTimeout(ofSeconds(30)).pollingEvery(ofMillis(500)); assertTrue(wait.until(searchingFunction.compose(contextFunction)).size() > 0); }
@Override public WebElement findElement(SearchContext context) { AppiumFunction<SearchContext, WebElement> searchingFunction = null; for (By by: bys) { searchingFunction = Optional.ofNullable(searchingFunction != null ? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by)); } FluentWait<SearchContext> waiting = new FluentWait<>(context); try { checkNotNull(searchingFunction); return waiting.until(searchingFunction); } catch (TimeoutException e) { throw new NoSuchElementException("Cannot locate an element using " + toString()); } } }
@Test public void complexWaitingTestWithPreCondition() { AppiumFunction<Pattern, List<WebElement>> compositeFunction = searchingFunction.compose(contextFunction); Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW")) .withTimeout(ofSeconds(30)); List<WebElement> elements = wait.until(compositeFunction); assertThat("Element size should be 1", elements.size(), is(1)); assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW")); }
/** * Repeatedly applies this instance's input value to the given function until one of the following occurs: * * <ul> * <li> the function returns neither null nor false,</li> * <li> the function throws an unignored exception,</li> * <li> the default timeout expires</li> * </ul> * * useful in situations where FluentAdapter#await is too specific * (for example to check against page source or title) * * @param f function to execute * @param <T> the return type * @return the return value. */ public <T>T waitUntil(Function<WebDriver, T> f) { FluentWait<WebDriver> wait = fluentWait().withTimeout(Duration.ofMillis(3000)); return waitUntil(wait,f); }
/** * Creates a generic {@code FluentWait<WebDriver>} instance * using the underlying web driver */ public FluentWait<WebDriver> fluentWait() { return new FluentWait<WebDriver>(super.getDriver()); }
@Override public SebContextWait ignoring(Class<? extends Throwable> firstType, Class<? extends Throwable> secondType) { return (SebContextWait) super.ignoring(firstType, secondType); }
@Test(expected = TimeoutException.class) public void testDefaultStrategy() { final FakeElement el = new FakeElement(); final Wait<FakeElement> wait = new AppiumFluentWait<>(el, Clock.systemDefaultZone(), duration -> { assertThat(duration.getSeconds(), is(equalTo(1L))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(AppiumFluentWait.IterationInfo::getInterval) .withTimeout(ofSeconds(3)) .pollingEvery(ofSeconds(1)); wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); }
private void waitFor(final Function<PostEntity, Boolean> predicate) { new FluentWait<>(postEntity).withTimeout(DiscussionsConstants.TIMEOUT, TimeUnit.SECONDS) .until(predicate); }
/** * 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 * * Useful in situations where FluentAdapter#await is too specific * (for example to check against page source) * * @param <T> the return type * @param wait generic {@code FluentWait<WebDriver>} instance * @param f function to execute * @return the return value */ public <T>T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }
public FluentWait<WebDriver> driverFluentWait(int timeoutInSeconds) { FluentWait<WebDriver> fw = null; for (int i = 0; i < 10; i++) { try { fw = new FluentWait<>(this.driver).withTimeout(timeoutInSeconds, TimeUnit.SECONDS); fw.ignoring(WebDriverException.class, ClassCastException.class); fw.ignoring(NoSuchElementException.class); return fw; } catch (Exception e) { if (i >= 9) { logger.warn("driverFluentWait Failed attempt : " + i + "/n" + e); } } } if (fw == null) { throw new WebDriverException("driverFluentWait failed after ten attempts"); } else { return fw; } }
default Wait<WebDriver> fluentWait() { return new FluentWait<>(TestBase.driver()) .withTimeout(1, TimeUnit.MINUTES) .pollingEvery(5, TimeUnit.MILLISECONDS) .ignoreAll(new ArrayList<Class<? extends Throwable>>() { { add(StaleElementReferenceException.class); add(NoSuchElementException.class); add(TimeoutException.class); add(InvalidElementStateException.class); add(WebDriverException.class); } }).withMessage("The message you will see in if a TimeoutException is thrown"); }
@Test(expected = TimeoutException.class) public void nullPointerExceptionSafetyTestWithPostConditions() { Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context")) .withTimeout(ofSeconds(30)).pollingEvery(ofMillis(500)); assertTrue(wait.until(contextFunction.andThen(searchingFunction).andThen(filteringFunction)).size() > 0); } }
@Override public WebElement findElement(SearchContext context) { AppiumFunction<SearchContext, WebElement> searchingFunction = null; for (By by: bys) { searchingFunction = Optional.ofNullable(searchingFunction != null ? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by)); } FluentWait<SearchContext> waiting = new FluentWait<>(context); try { checkNotNull(searchingFunction); return waiting.until(searchingFunction); } catch (TimeoutException e) { throw new NoSuchElementException("Cannot locate an element using " + toString()); } } }
}); Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW")) .withTimeout(ofSeconds(30)); List<WebElement> elements = wait.until(compositeFunction); assertThat("Element size should be 1", elements.size(), is(1));