private static ExpectedCondition<Boolean> clickUntilFound(final By locatorToClick, final By locatorToFind) { return new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { driver.findElement(locatorToClick).click(); Thread.sleep(xxx); //some small delay here to prevent rapid refresh DOS'ing your site if (driver.findElements(locatorToFind).size() > 0) return driver.findElement(locatorToFind).isDisplayed(); else return false; } public String toString() { return String.format("element found by %s after clicking %s", locatorToFind, locatorToClick); } };}
Usage:WebDriverWait wait = new WebDriverWait(driver, waitTimeout);wait.until(clickUntilFound(refreshButton, acceptButton));
private static ExpectedCondition<Boolean> refreshUntilFound(final By locatorToFind) { return new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { driver().navigate().refresh(); Tread.sleep(xxx) //some small delay to prevent DOS if (driver.findElements(locatorToFind).size() > 0) return driver.findElement(locatorToFind).isDisplayed(); else return false; } public String toString() { return String.format("element found by %s after refresh", locatorToFind); } };}
WebDriverWait wait = new WebDriverWait(driver, waitTimeout);wait.until(refreshUntilFound(acceptButton));