Search in sources :

Example 1 with SystemClock

use of org.openqa.selenium.support.ui.SystemClock 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 SystemClock

use of org.openqa.selenium.support.ui.SystemClock 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)

Aggregations

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 Duration (org.openqa.selenium.support.ui.Duration)2 SystemClock (org.openqa.selenium.support.ui.SystemClock)2 Wait (org.openqa.selenium.support.ui.Wait)2