selenium设置等待的方法java,Java&Selenium智能等待方法封装

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);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值