@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"); }
@Test public void testCustomStrategyOverridesDefaultInterval() { final FakeElement el = new FakeElement(); final AtomicInteger callsCounter = new AtomicInteger(0); final Wait<FakeElement> wait = new AppiumFluentWait<>(el, Clock.systemDefaultZone(), duration -> { callsCounter.incrementAndGet(); assertThat(duration.getSeconds(), is(equalTo(2L))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(info -> ofSeconds(2)) .withTimeout(ofSeconds(3)) .pollingEvery(ofSeconds(1)); try { wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); } catch (TimeoutException e) { // this is expected assertThat(callsCounter.get(), is(equalTo(2))); } }
@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); }
@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, Clock.systemDefaultZone(), duration -> { int callNumber = callsCounter.incrementAndGet(); assertThat(duration.getSeconds(), is(equalTo(pollingStrategy.apply((long) callNumber)))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(info -> ofSeconds(pollingStrategy.apply(info.getNumber()))) .withTimeout(ofSeconds(4)) .pollingEvery(ofSeconds(1)); try { wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); } catch (TimeoutException e) { // this is expected assertThat(callsCounter.get(), is(equalTo(2))); } } }
@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); } }
@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")); }
List<WebElement> elements = wait.until(compositeFunction); assertThat("Element size should be 1", elements.size(), is(1)); assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW"));
Wait wait = new WebDriverWait(driver, Timeouts.WAIT_FOR_PAGE_TO_LOAD_TIMEOUT); WebElement webElement = (WebElement) wait.until( Helper.oneOfElementsLocatedVisible( By.xpath(SERVICE_TITLE_LOCATOR), By.xpath(ATTENTION_REQUEST_ALREADY_PRESENTS_WINDOW_LOCATOR) ) );
public Boolean isElemAttribContaining(WebElement element, String attribute, String attributeValue) { try { return fluentWait.until(attributeContains(element, attribute, attributeValue)); } catch (Exception e) { return FALSE; } }
public Boolean isPageUrlContaining(String text) { try { return fluentWait.until(urlContains(text)); } catch (Exception e) { return FALSE; } }
public Boolean isTitleEqualTo(String text) { try { return fluentWait.until(titleIs(text)); } catch (Exception e) { return FALSE; } }
@Override public List<WebElement> findElements(By by) { try { return fluentWait.until(presenceOfAllElementsLocatedBy(by)); } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
public List<WebElement> findNestedElements(By parent, By child) { try { return fluentWait.until(presenceOfNestedElementsLocatedBy(parent, child)); } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
public Alert getAlert() { try { return fluentWait.until(alertIsPresent()); } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
public Boolean isElemInvisible(WebElement element) { try { return fluentWait.until(invisibilityOf(element)); } catch (Exception e) { return FALSE; } }
@Override public WebElement findElement(By by) { try { WebElement element = fluentWait.until(presenceOfElementLocated(by)); focus(element); return element; } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
public WebElement findNestedElement(By parent, By child) { try { WebElement element = fluentWait.until(presenceOfNestedElementLocatedBy(parent, child)); focus(element); return element; } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
public Boolean isElemAttribNotContaining(WebElement element, String attribute, String attributeValue) { try { return fluentWait.until(expectedCondition(!getAttribute(element, attribute).contains(attributeValue))); } catch (Exception e) { return FALSE; } }
public Boolean isElemTextLengthMoreThan(WebElement element, Integer length) { try { return fluentWait.until(expectedCondition(getElementText(element).length() > length)); } catch (Exception e) { return FALSE; } }
public Boolean isElemValueLengthMoreThan(WebElement element, Integer length) { try { return fluentWait.until(expectedCondition(getElementValue(element).length() > length)); } catch (Exception e) { return FALSE; } }