Search in sources :

Example 1 with Duration

use of org.openqa.selenium.support.ui.Duration in project java-client by appium.

the class AppiumFluentWaitTest method testIntervalCalculationForCustomStrategy.

@Test
public void testIntervalCalculationForCustomStrategy() {
    final FakeElement el = new FakeElement();
    final AtomicInteger callsCounter = new AtomicInteger(0);
    // Linear dependency
    final Function<Long, Long> pollingStrategy = x -> x * 2;
    final Wait<FakeElement> wait = new AppiumFluentWait<>(el, new SystemClock(), duration -> {
        int callNumber = callsCounter.incrementAndGet();
        assertThat(duration.in(TimeUnit.SECONDS), is(equalTo(pollingStrategy.apply((long) callNumber))));
        Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
    }).withPollingStrategy(info -> new Duration(pollingStrategy.apply(info.getNumber()), TimeUnit.SECONDS)).withTimeout(4, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
    try {
        wait.until(FakeElement::isDisplayed);
        Assert.fail("TimeoutException is expected");
    } catch (TimeoutException e) {
        // this is expected
        assertThat(callsCounter.get(), is(equalTo(2)));
    }
}
Also used : Duration(org.openqa.selenium.support.ui.Duration) Wait(org.openqa.selenium.support.ui.Wait) SystemClock(org.openqa.selenium.support.ui.SystemClock) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Test(org.junit.Test) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(org.openqa.selenium.TimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) AppiumFluentWait(io.appium.java_client.AppiumFluentWait) SystemClock(org.openqa.selenium.support.ui.SystemClock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(org.openqa.selenium.support.ui.Duration) TimeoutException(org.openqa.selenium.TimeoutException) Test(org.junit.Test)

Example 2 with Duration

use of org.openqa.selenium.support.ui.Duration in project java-client by appium.

the class AppiumFluentWaitTest method testCustomStrategyOverridesDefaultInterval.

@Test
public void testCustomStrategyOverridesDefaultInterval() {
    final FakeElement el = new FakeElement();
    final AtomicInteger callsCounter = new AtomicInteger(0);
    final Wait<FakeElement> wait = new AppiumFluentWait<>(el, new SystemClock(), duration -> {
        callsCounter.incrementAndGet();
        assertThat(duration.in(TimeUnit.SECONDS), is(equalTo(2L)));
        Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
    }).withPollingStrategy(info -> new Duration(2, TimeUnit.SECONDS)).withTimeout(3, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
    try {
        wait.until(FakeElement::isDisplayed);
        Assert.fail("TimeoutException is expected");
    } catch (TimeoutException e) {
        // this is expected
        assertThat(callsCounter.get(), is(equalTo(2)));
    }
}
Also used : Duration(org.openqa.selenium.support.ui.Duration) Wait(org.openqa.selenium.support.ui.Wait) SystemClock(org.openqa.selenium.support.ui.SystemClock) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Test(org.junit.Test) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(org.openqa.selenium.TimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) AppiumFluentWait(io.appium.java_client.AppiumFluentWait) SystemClock(org.openqa.selenium.support.ui.SystemClock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(org.openqa.selenium.support.ui.Duration) TimeoutException(org.openqa.selenium.TimeoutException) Test(org.junit.Test)

Example 3 with Duration

use of org.openqa.selenium.support.ui.Duration in project java-client by appium.

the class AppiumFluentWait method until.

/**
 * Repeatedly applies this instance's input value to the given function until one of the following
 * occurs:
 * <ol>
 * <li>the function returns neither null nor false,</li>
 * <li>the function throws an unignored exception,</li>
 * <li>the timeout expires,</li>
 * <li>the current thread is interrupted</li>
 * </ol>.
 *
 * @param isTrue the parameter to pass to the expected condition
 * @param <V>    The function's expected return type.
 * @return The functions' return value if the function returned something different
 *         from null or false before the timeout expired.
 * @throws TimeoutException If the timeout expires.
 */
@Override
public <V> V until(Function<? super T, V> isTrue) {
    final long start = getClock().now();
    final long end = getClock().laterBy(getTimeout().in(TimeUnit.MILLISECONDS));
    long iterationNumber = 1;
    Throwable lastException;
    while (true) {
        try {
            V value = isTrue.apply(getInput());
            if (value != null && (Boolean.class != value.getClass() || Boolean.TRUE.equals(value))) {
                return value;
            }
            // Clear the last exception; if another retry or timeout exception would
            // be caused by a false or null value, the last exception is not the
            // cause of the timeout.
            lastException = null;
        } catch (Throwable e) {
            lastException = propagateIfNotIgnored(e);
        }
        // with a zero timeout can succeed.
        if (!getClock().isNowBefore(end)) {
            String message = getMessageSupplier() != null ? getMessageSupplier().get() : null;
            String timeoutMessage = String.format("Expected condition failed: %s (tried for %d second(s) with %s interval)", message == null ? "waiting for " + isTrue : message, getTimeout().in(TimeUnit.SECONDS), getInterval());
            throw timeoutException(timeoutMessage, lastException);
        }
        try {
            Duration interval = getInterval();
            if (pollingStrategy != null) {
                final IterationInfo info = new IterationInfo(iterationNumber, new Duration(getClock().now() - start, TimeUnit.MILLISECONDS), getTimeout(), interval);
                interval = pollingStrategy.apply(info);
            }
            getSleeper().sleep(interval);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new WebDriverException(e);
        }
        ++iterationNumber;
    }
}
Also used : Duration(org.openqa.selenium.support.ui.Duration) WebDriverException(org.openqa.selenium.WebDriverException)

Aggregations

Duration (org.openqa.selenium.support.ui.Duration)3 AppiumFluentWait (io.appium.java_client.AppiumFluentWait)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Is.is (org.hamcrest.core.Is.is)2 IsEqual.equalTo (org.hamcrest.core.IsEqual.equalTo)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 TimeoutException (org.openqa.selenium.TimeoutException)2 SystemClock (org.openqa.selenium.support.ui.SystemClock)2 Wait (org.openqa.selenium.support.ui.Wait)2 WebDriverException (org.openqa.selenium.WebDriverException)1