packageutil;importorg.openqa.selenium.By;importorg.openqa.selenium.TimeoutException;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.ui.ExpectedCondition;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;importorg.testng.Assert;importorg.testng.Reporter;import staticutil.LogUtil.info;public classWaitElementUtil {//public WebDriver driver = null;//private int timeOutInSeconds =10;
/**用于测试执行过程中暂停程序执行的等待方法*/
public static void sleep(longmillsecond){try{
Thread.sleep(millsecond);
Reporter.log("强制等待"+ millsecond + "毫秒...");
}catch(Exception e){
e.printStackTrace();
}
}/*** 同上
*@paramdriver
*@paramby
*@paramtimeOutInSeconds*/
public static void waitWebElementPresence(WebDriver driver, By by, inttimeOutInSeconds){
WebDriverWait wait= newWebDriverWait(driver, timeOutInSeconds);
WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(by));
}/*** 显示等待页面元素可被点击状态,参数为页面元素xpath定位表达式
*@paramdriver
*@paramby
*@paramtimeOutInSeconds*/
public static void waitWebElementToBeClickable(WebDriver driver, By by, inttimeOutInSeconds){
WebDriverWait waitElement= newWebDriverWait(driver, timeOutInSeconds);
WebElement element=waitElement.until(ExpectedConditions.elementToBeClickable(by));
}/*** 显示等待页面元素被选中状态,参数为页面元素xpath定位表达式
*@paramdriver
*@paramxpathExpression
*@paramtimeOutInSeconds*/
public static void waitWebElementToBeSelected(WebDriver driver, String xpathExpression, inttimeOutInSeconds){
WebDriverWait waitElement= newWebDriverWait(driver, timeOutInSeconds);
Boolean element=waitElement.until(ExpectedConditions.elementToBeSelected(By.xpath(xpathExpression)));
}/*** 显示等待页面元素出现,参数为页面元素xpath定位表达式
*@paramdriver
*@paramxpathExpression
*@paramtext
*@paramtimeOutInSeconds*/
public static void waitWebElementToBePresentInElementValue(WebDriver driver, String xpathExpression, String text, inttimeOutInSeconds){
WebDriverWait waitElement= newWebDriverWait(driver, timeOutInSeconds);
Boolean element=waitElement.until(ExpectedConditions.textToBePresentInElementValue(By.xpath(xpathExpression), text));
}/*** 显示等待页面标题包含"title"元素出现,参数为页面元素xpath定位表达式
*@paramdriver
*@paramtitle
*@paramtimeOutInSeconds*/
public static void waitWebElementTitleContains(WebDriver driver, String title, inttimeOutInSeconds){
WebDriverWait waitElement= newWebDriverWait(driver, timeOutInSeconds);
Boolean element=waitElement.until(ExpectedConditions.titleContains(title));
}/*** 显示等待页面元素加载
*@paramby:等待元素elementName加载完成
*@paramtimeOutInSeconds:等待时常
*@throwsException*/
public static void waitElement(WebDriver driver, By by, int timeOutInSeconds) throwsException {try{
waitWebElementPresence(driver, by, timeOutInSeconds);
info("显示等待页面元素出现成功, 页面元素是" +by);
}catch(Exception e){
info("显示等待页面元素时出现异常,异常信息为:" +e.getMessage());
e.printStackTrace();
}
}/*** 显示等待元素加载
*@paramdriver:浏览器驱动
*@paramtimeOut:等待时常
*@paramby:元素定位*/
public static void intelligentWait(WebDriver driver,int timeOut, finalBy by) {try{
(new WebDriverWait(driver, timeOut)).until(new ExpectedCondition() {publicBoolean apply(WebDriver driver) {
WebElement element=driver.findElement(by);returnelement.isDisplayed();
}
});
}catch(TimeoutException e) {
Assert.fail("超时L !! " + timeOut + " 秒之后还没找到元素 [" + by + "]", e);
}
}
}