下面列出了org.openqa.selenium.support.ui.FluentWait#until ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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;
}
});
}
@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());
}
}
@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");
}
@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");
}
@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");
}
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();
});
}
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();
});
}
/**
* 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;
});
}
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;
}
}
@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");
}
@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");
}
@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");
}
/**
* 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);
}