首页 技术 正文
技术 2022年11月6日
0 收藏 446 点赞 527 浏览 3160 个字

Java&Selenium智能等待方法封装

ExpectedConditions方法还有很多,自然也可以继续扩展很多

package util;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.Reporter;import static util.LogUtil.info;public class WaitElementUtil {
//public WebDriver driver = null;
//private int timeOutInSeconds =10;
/**用于测试执行过程中暂停程序执行的等待方法*/
public static void sleep(long millsecond){
try{
Thread.sleep(millsecond);
Reporter.log("强制等待"+ millsecond + "毫秒...");
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 同上
* @param driver
* @param by
* @param timeOutInSeconds
*/
public static void waitWebElementPresence(WebDriver driver, By by, int timeOutInSeconds){
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(by));
} /**
* 显示等待页面元素可被点击状态,参数为页面元素xpath定位表达式
* @param driver
* @param by
* @param timeOutInSeconds
*/
public static void waitWebElementToBeClickable(WebDriver driver, By by, int timeOutInSeconds){
WebDriverWait waitElement = new WebDriverWait(driver, timeOutInSeconds);
WebElement element = waitElement.until(ExpectedConditions.elementToBeClickable(by));
} /**
* 显示等待页面元素被选中状态,参数为页面元素xpath定位表达式
* @param driver
* @param xpathExpression
* @param timeOutInSeconds
*/
public static void waitWebElementToBeSelected(WebDriver driver, String xpathExpression, int timeOutInSeconds){
WebDriverWait waitElement = new WebDriverWait(driver, timeOutInSeconds);
Boolean element = waitElement.until(ExpectedConditions.elementToBeSelected(By.xpath(xpathExpression)));
} /**
* 显示等待页面元素出现,参数为页面元素xpath定位表达式
* @param driver
* @param xpathExpression
* @param text
* @param timeOutInSeconds
*/
public static void waitWebElementToBePresentInElementValue(WebDriver driver, String xpathExpression, String text, int timeOutInSeconds){
WebDriverWait waitElement = new WebDriverWait(driver, timeOutInSeconds);
Boolean element = waitElement.until(ExpectedConditions.textToBePresentInElementValue(By.xpath(xpathExpression), text));
} /**
* 显示等待页面标题包含"title"元素出现,参数为页面元素xpath定位表达式
* @param driver
* @param title
* @param timeOutInSeconds
*/
public static void waitWebElementTitleContains(WebDriver driver, String title, int timeOutInSeconds){
WebDriverWait waitElement = new WebDriverWait(driver, timeOutInSeconds);
Boolean element = waitElement.until(ExpectedConditions.titleContains(title));
}
/**
* 显示等待页面元素加载
* @param by:等待元素elementName加载完成
* @param timeOutInSeconds:等待时常
* @throws Exception
*/
public static void waitElement(WebDriver driver, By by, int timeOutInSeconds) throws Exception {
try{
waitWebElementPresence(driver, by, timeOutInSeconds);
info("显示等待页面元素出现成功, 页面元素是" + by);
}catch (Exception e){
info("显示等待页面元素时出现异常,异常信息为:" + e.getMessage());
e.printStackTrace();
}
} /**
* 显示等待元素加载
* @param driver:浏览器驱动
* @param timeOut:等待时常
* @param by:元素定位
*/
public static void intelligentWait(WebDriver driver,int timeOut, final By by) {
try {
(new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(by);
return element.isDisplayed();
}
});
} catch (TimeoutException e) {
Assert.fail("超时L !! " + timeOut + " 秒之后还没找到元素 [" + by + "]", e);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905