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)));
}
}
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)));
}
}
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;
}
}
Aggregations