Java Code Examples for org.openqa.selenium.support.ui.FluentWait#until()

The following examples show how to use org.openqa.selenium.support.ui.FluentWait#until() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppiumUtils.java    From Quantum with MIT License 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "deprecation" })
private static MobileElement fluentWait(AppiumDriver driver, By xpath) {
	MobileElement waitElement = null;

	FluentWait<RemoteWebDriver> fwait = new FluentWait<RemoteWebDriver>(driver).withTimeout(15, TimeUnit.SECONDS)
			.pollingEvery(500, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class)
			.ignoring(TimeoutException.class);

	try {
		waitElement = (MobileElement) fwait.until(new Function<RemoteWebDriver, WebElement>() {
			public WebElement apply(RemoteWebDriver driver) {
				return driver.findElement(xpath);
			}
		});
	} catch (Exception e) {
	}
	return waitElement;
}
 
Example 2
Source File: TestWebElementRenderChecker.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private void waitElementIsStatic(FluentWait<WebDriver> webDriverWait, WebElement webElement) {
  AtomicInteger sizeHashCode = new AtomicInteger();

  webDriverWait.until(
      (ExpectedCondition<Boolean>)
          driver -> {
            Dimension newDimension = waitAndGetWebElement(webElement).getSize();

            if (dimensionsAreEquivalent(sizeHashCode, newDimension)) {
              return true;
            } else {
              sizeHashCode.set(getSizeHashCode(newDimension));
              return false;
            }
          });
}
 
Example 3
Source File: ByChained.java    From java-client with Apache License 2.0 6 votes vote down vote up
@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());
    }
}
 
Example 4
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public WebElement getClickableElementFoundBy(
		final boolean valueAlias,
		@NotNull final String value,
		@NotNull final FeatureState featureState,
		final long waitTime) {

	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	while (time < waitTime * Constants.MILLISECONDS_PER_SECOND) {
		for (final String locationMethod : LOCATION_METHODS) {
			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.elementToBeClickable(by);

				return wait.until(condition);
			} catch (final Exception ignored) {
				/*
					Do nothing
				 */
			}

			time += Constants.TIME_SLICE;
		}
	}

	throw new WebElementException("All attempts to find element failed");
}
 
Example 5
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public WebElement getVisibleElementFoundBy(
		final boolean valueAlias,
		@NotNull final String value,
		@NotNull final FeatureState featureState,
		final long waitTime) {

	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);
	checkArgument(waitTime >= 0);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	do {
		for (final String locationMethod : LOCATION_METHODS) {
			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.visibilityOfElementLocated(by);

				return wait.until(condition);
			} catch (final Exception ignored) {
				/*
					Do nothing
				 */
			}

			time += Constants.TIME_SLICE;
		}

	} while (time < waitTime * Constants.MILLISECONDS_PER_SECOND);

	throw new WebElementException("All attempts to find element failed");
}
 
Example 6
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public WebElement getPresenceElementFoundBy(
		final boolean valueAlias,
		@NotNull final String value,
		@NotNull final FeatureState featureState,
		final long waitTime) {

	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	while (time < waitTime * Constants.MILLISECONDS_PER_SECOND) {
		for (final String locationMethod : LOCATION_METHODS) {
			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.presenceOfElementLocated(by);

				return wait.until(condition);
			} catch (final Exception ignored) {
				/*
					Do nothing
				 */
			}

			time += Constants.TIME_SLICE;
		}
	}

	throw new WebElementException("All attempts to find element failed");
}
 
Example 7
Source File: OrganizationListPage.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void waitForElementDisappearance(String xpath) {
  FluentWait<SeleniumWebDriver> wait =
      new FluentWait<>(seleniumWebDriver)
          .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
          .pollingEvery(200, TimeUnit.MILLISECONDS)
          .ignoring(StaleElementReferenceException.class);

  wait.until(
      (Function<WebDriver, Boolean>)
          driver -> {
            List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
            return elements.isEmpty() || !elements.get(0).isDisplayed();
          });
}
 
Example 8
Source File: AddMember.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void waitForElementDisappearance(String xpath) {
  FluentWait<WebDriver> wait =
      new FluentWait<WebDriver>(seleniumWebDriver)
          .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
          .pollingEvery(200, TimeUnit.MILLISECONDS)
          .ignoring(StaleElementReferenceException.class);

  wait.until(
      (Function<WebDriver, Boolean>)
          driver -> {
            List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
            return elements.isEmpty() || elements.get(0).isDisplayed();
          });
}
 
Example 9
Source File: AddMember.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Wait for popup is attached to DOM and animation ends.
 *
 * @param xpath xpath to match the 'che-popup' element
 */
private void waitForPopupAppearence(String xpath) {
  FluentWait<WebDriver> wait =
      new FluentWait<WebDriver>(seleniumWebDriver)
          .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
          .pollingEvery(200, TimeUnit.MILLISECONDS)
          .ignoring(StaleElementReferenceException.class);

  Map<String, Integer> lastSize = new HashMap<>();
  lastSize.put("height", 0);
  lastSize.put("width", 0);

  wait.until(
      (Function<WebDriver, Boolean>)
          driver -> {
            List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
            if (elements.isEmpty()) {
              return false;
            }

            Dimension size = elements.get(0).getSize();
            if (lastSize.get("height") < size.getHeight()
                || lastSize.get("width") < size.getHeight()) {
              lastSize.put("height", size.getHeight());
              lastSize.put("width", size.getWidth());

              return false;
            }

            return true;
          });
}
 
Example 10
Source File: AppiumElementLocator.java    From java-client with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 11
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
@Override
public void getNotClickableElementFoundBy(
	final boolean valueAlias,
	final String value,
	final FeatureState featureState,
	final long waitTime) {
	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	mainloop:
	while (time < waitTime * Constants.MILLISECONDS_PER_SECOND) {
		for (final String locationMethod : LOCATION_METHODS) {

			time += Constants.TIME_SLICE;

			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.elementToBeClickable(by);

				final WebElement element = wait.until(condition);

				/*
					If we found an element, drop back to the while loop
				 */
				if (element != null) {
					break mainloop;
				}
			} catch (final Exception ignored) {
				/*
					We expect missing elements to timeout with an exception
				 */
			}
		}

		/*
			If we got here, none of the locations returned an element
		 */
		return;
	}

	throw new WebElementException("Timeout waiting for elements to not be visible");
}
 
Example 12
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
@Override
public void getNotVisibleElementFoundBy(
		final boolean valueAlias,
		@NotNull final String value,
		@NotNull final FeatureState featureState,
		final long waitTime) {

	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	mainloop:
	while (time < waitTime * Constants.MILLISECONDS_PER_SECOND) {
		for (final String locationMethod : LOCATION_METHODS) {

			time += Constants.TIME_SLICE;

			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.visibilityOfElementLocated(by);

				final WebElement element = wait.until(condition);

				/*
					If we found an element, drop back to the while loop
				 */
				if (element != null) {
					break mainloop;
				}
			} catch (final Exception ignored) {
				/*
					We expect missing elements to timeout with an exception
				 */
			}
		}

		/*
			If we got here, none of the locations returned an element
		 */
		return;
	}

	throw new WebElementException("Timeout waiting for elements to not be visible");
}
 
Example 13
Source File: SimpleWebElementInteractionImpl.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
@Override
public void getNotPresenceElementFoundBy(
		final boolean valueAlias,
		@NotNull final String value,
		@NotNull final FeatureState featureState,
		final long waitTime) {

	checkArgument(StringUtils.isNotBlank(value));
	checkNotNull(featureState);

	final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();
	long time = 0;

	mainloop:
	while (time < waitTime * Constants.MILLISECONDS_PER_SECOND) {
		for (final String locationMethod : LOCATION_METHODS) {

			time += Constants.TIME_SLICE;

			try {
				final By by = getBy.getBy(locationMethod, valueAlias, value, featureState);
				final FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
					.withTimeout(Duration.ofMillis(Constants.TIME_SLICE));
				final ExpectedCondition<WebElement> condition =
					ExpectedConditions.presenceOfElementLocated(by);

				final WebElement element = wait.until(condition);

				/*
					If we found an element, drop back to the while loop
				 */
				if (element != null) {
					break mainloop;
				}
			} catch (final Exception ignored) {
				/*
					We expect missing elements to timeout with an exception
				 */
			}
		}

		/*
			If we got here, none of the locations returned an element
		 */
		return;
	}

	throw new WebElementException("Timeout waiting for elements to not be present");
}
 
Example 14
Source File: SeleniumHelper.java    From hsac-fitnesse-fixtures with Apache License 2.0 2 votes vote down vote up
/**
 * Executes condition until it returns a value other than null or false.
 * It does not forward StaleElementReferenceExceptions, but keeps waiting.
 * @param maxSecondsToWait number of seconds to wait at most.
 * @param condition condition to check.
 * @param <T> return type.
 * @return result of condition (if not null).
 * @throws TimeoutException when condition did not give a value to return after maxSecondsToWait.
 */
public <T> T waitUntil(int maxSecondsToWait, ExpectedCondition<T> condition) {
    ExpectedCondition<T> cHandlingStale = getConditionIgnoringStaleElement(condition);
    FluentWait<WebDriver> wait = waitDriver().withTimeout(Duration.ofSeconds(maxSecondsToWait));
    return wait.until(cHandlingStale);
}