Java&Selenium智能等待方法封装
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);
}
}
}
Java&Selenium智能等待方法封装的更多相关文章
- Python&Selenium智能等待方法封装
摘要:本篇博文用几行代码展示Python和Selenium做自动化测试时常见的显示等待和封装 # 用于实现智能等待页面元素的出现 # encoding = utf-8 ""&quo ...
- Java&Selenium 模拟键盘方法封装
Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...
- Java&Selenium控制滚动条方法封装
Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...
- Java&Selenium 模拟鼠标方法封装
Java&Selenium 模拟鼠标方法封装 package util; import org.openqa.selenium.By; import org.openqa.selenium.W ...
- selenium时间等待方法
在UI自动化测试中,必然会遇到环境不稳定.网络慢等情况.当你觉得定位没有问题,但程序却直接报了元素不可见时,那你就需要思考是否因为程序运行太快或者页面加载太慢而造成了元素不可见,必须要再等待直至元素可 ...
- java selenium (十三) 智能等待页面加载完成
我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作. 否则页面上的元素不存在,会抛出异常. 或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操 ...
- Java Selenium (十二) 操作弹出窗口 & 智能等待页面加载完成 & 处理 Iframe 中的元素
一.操作弹出窗口 原理 在代码里, 通过 Set<String> allWindowsId = driver.getWindowHandles(); 来获取到所有弹出浏览器的句柄, 然 ...
- Java&Selenium截图方法封装
Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...
- 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...
随机推荐
- iostat的坑
简单使用iostat查询io使用量,会让你看不懂所以然,因为很多人疏忽了这个命令查到的结果根本不是实际值,需要注意的是一句话: “第1次采样信息与单独执行iostat的效果一样,为从系统开机到当前执行 ...
- C#使用KingAOP实现AOP面向切面编程二
本文继续上篇讲述一下比较复杂点的AOP例子,先新建一个控制台项目,然后同样先在Nuget中搜索安装KingAop到项目中 1.项目结构 2 .定义一个登录实体类User和LoggingAspect切面 ...
- 用BERT做语义相似度匹配任务:计算相似度的方式
1. 自然地使用[CLS] 2. cosine similairity 3. 长短文本的区别 4. sentence/word embedding 5. siamese network 方式 1. 自 ...
- 在同一个页面多次调用el-select选择器
elementui是一个十分好用的组件库,但毕竟也不能做到面面俱到,有些还是要根据根据自己的实际需求做一些自定义的方法. 比如el-select选择器在同一个页面使用多次的话就会造成选择一个另一个也会 ...
- Qt 和 Boost关于信号和槽的对比说明
对比 无论是 Qt 的实现方式还是 Boost 的实现方式,除了必须的定义信号和槽的类之外,都不需要额外的类. 两种实现都解决了类爆炸的问题.下面让我们对照着来看一下我们前面的分析. 两个不同的术语以 ...
- python 计算文件夹里所有内容的大小总和
计算文件夹里所有内容的大小总和 递归方法 '''计算文件夹的大小''' import os def dir_file_size(path): if os.path.isdir(path): file_ ...
- 5、1 es 数据库和mysql 数据库同步 (Windows操作系统)
(1)在logstash-5.6.8安装目录下创建文件夹mysqletc (名称随意) (2)文件夹下创建mysql.conf (名称随意) ,内容如下: input { jdbc { #sc表 jd ...
- PO,VO,DAO,BO,POJO之间的区别与解释
VO value object:值对象 通常用于业务层之间的数据传递,由new创建,由GC回收. PO persistant object:持久层对象 对应数据库中表的字段. VO和PO,都是属性加上 ...
- final关键字、多态 (札记)
目录 protected fianl 子父类中同名的 private 方法 java中的前期绑定 免疫多态 谁先被执行,构造器 还是 初始化? 协变返回类型 <Thinking in java& ...
- Yii2.0 RESTful API 之速率限制
Yii2.0 RESTFul API 之速率限制 什么是速率限制? 权威指南翻译过来为限流,为防止滥用,你应该考虑对您的 API 限流. 例如,您可以限制每个用户 10 分钟内最多调用 API 100 ...