Skip to content

Instantly share code, notes, and snippets.

@framiere
Created October 1, 2013 16:13
Show Gist options
  • Save framiere/6780992 to your computer and use it in GitHub Desktop.
Save framiere/6780992 to your computer and use it in GitHub Desktop.
public void waitUntilRemoved(final By by) {
try {
until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver from) {
from.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
try {
return from.findElements(by).isEmpty();
} catch (NoSuchElementException e) {
return true;
} finally {
from.manage().timeouts().implicitlyWait(driverWaitBeforeStopInSeconds, TimeUnit.SECONDS);
}
}
});
} catch (RuntimeException e) {
error("not removed " + by, e);
}
}
public void waitUntilDisplayed(final WebElement webElement) {
try {
until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver from) {
return webElement.isDisplayed();
}
});
} catch (RuntimeException e) {
error("element " + webElement.getTagName() + " is not displayed", e);
}
}
public boolean until(Function<WebDriver, Boolean> function) {
// iterate until we have no more StaleElementReferenceException
while (true) {
try {
return tryUntil(function);
} catch (StaleElementReferenceException e) {
//
}
}
}
private boolean tryUntil(Function<WebDriver, Boolean> function) {
// test the function now
if (function.apply(webDriver)) {
return true;
}
// nope ? ok, once more
if (function.apply(webDriver)) {
return true;
}
// test for 1 second with very rapid tests
try {
if (new WebDriverWait(webDriver, 1).until(function)) {
return true;
}
} catch (Exception e) {
// no op
}
// ok it's still not ready, so let's wait
return new WebDriverWait(webDriver, driverWaitBeforeStopInSeconds).until(function);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment