1. webDriverWait自定义WebElement类事件
public WebElement waitForElementVisible(WebDriver driver,final By locator, long timeOutInSeconds, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement el = driver.findElement(locator);
if (el.isDisplayed()) {
return el;
}
} catch (Exception e) {
}
return null;
}
}; WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
}
2. WebDriverWait自定义Boolean类等待
public Boolean waitForElementPresent(WebDriver driver,final By locator, long timeOutInSeconds, String errorMessage) {
Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
driver.findElement(locator);
return true;
} catch (Exception e) {
return false;
}
}
}; WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
}
参考博客:http://www.cnblogs.com/testing-life/p/5242668.html