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智能等待方法封装的更多相关文章

  1. Python&Selenium智能等待方法封装

    摘要:本篇博文用几行代码展示Python和Selenium做自动化测试时常见的显示等待和封装 # 用于实现智能等待页面元素的出现 # encoding = utf-8 ""&quo ...

  2. Java&Selenium 模拟键盘方法封装

    Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...

  3. Java&Selenium控制滚动条方法封装

    Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...

  4. Java&Selenium 模拟鼠标方法封装

    Java&Selenium 模拟鼠标方法封装 package util; import org.openqa.selenium.By; import org.openqa.selenium.W ...

  5. selenium时间等待方法

    在UI自动化测试中,必然会遇到环境不稳定.网络慢等情况.当你觉得定位没有问题,但程序却直接报了元素不可见时,那你就需要思考是否因为程序运行太快或者页面加载太慢而造成了元素不可见,必须要再等待直至元素可 ...

  6. java selenium (十三) 智能等待页面加载完成

    我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作.  否则页面上的元素不存在,会抛出异常. 或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操 ...

  7. Java Selenium (十二) 操作弹出窗口 & 智能等待页面加载完成 & 处理 Iframe 中的元素

    一.操作弹出窗口   原理 在代码里, 通过 Set<String> allWindowsId = driver.getWindowHandles(); 来获取到所有弹出浏览器的句柄, 然 ...

  8. Java&Selenium截图方法封装

    Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...

  9. 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)

    1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...

随机推荐

  1. Linux内核编译、安装流程

    原文链接:https://blog.csdn.net/qq_28437139/article/details/83692907 此处只讲linux内核编译步骤至于安装虚拟机,安装ubuntu操作系统请 ...

  2. React native 放大点击区域 hitSlop属性的使用

    在日常的需求中,如上图的加减按钮,可能写ui布局的时候没考虑实际的这个点击范围太小,不利于真机上用户点击到,如果加包裹层加padding的话又会影响原先定好的布局,或者不利于对齐. 那么可以用  hi ...

  3. mongdb学习

    1.启动mongdb 服务(注启动之前把data 文件夹清空) 2.开发和jpa一样 只是extendMongoRepository 3.实体类只有id注解,属性全为String

  4. [转帖]linux下的find文件查找命令与grep文件内容查找命令

    linux下的find文件查找命令与grep文件内容查找命令 https://www.cnblogs.com/shileima/p/8431393.html 在使用linux时,经常需要进行文件查找. ...

  5. MySQL忘记密码解决

    1.设置管理员root密码为123 开启MySQL服务后 PS C:\WINDOWS\system32> mysqladmin -uroot -p password "123" ...

  6. MySQL的sql语言分类DML、DQL、DDL、DCL

    SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL 1.数据定义语言DDL(Data Definition Language) 对象: 数据库和表 ...

  7. Apache2.4+Tomcat7.0+php5.5整合配置详解

    在上一篇的基础上,继续添加php的配置 一.首先下载php5.5 首先下载php5.5,到官网下载http://www.php.net/downloads.php,参考http://www.cnblo ...

  8. C# xml序列化 datatime字段

    [XmlIgnore] public DateTime ApplicationDatetime { get; set; } [XmlElement("ApplicationDatetime& ...

  9. (四)springmvc之获取servlet原生对象

    一.使用DI注入的方式 <a href="<%=request.getContextPath()%>/servletObj_1">DI注入的方式</a ...

  10. sql 触发器里,发生错误,回滚提示错误语句

    SET @msg='错误消息';                RAISERROR(@msg, 16, 1);                ROLLBACK TRANSACTION;         ...