Automation 的 Wait 工具
public static WebDriverWait createWait(WebDriver driver) {
return new WebDriverWait(driver, Environment.getTimeOutInSeconds(),Environment.getSleepInMillis());
} --------------------------------------------------------------------------------
// <1> Wait for element present in Dom, may not visible /**
* Wait for element present in DOM, may not visible
* @param driver
* @param locator
* @param errorMessage
* @return
*/
1).
public static WebElement waitForElementPresent(WebDriver driver,final By locator, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage); // withMessage() 为 WebDriverWait 类的方法
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static WebElement waitForElementPresent(WebDriver driver, final By locator) {
return waitForElementPresent(driver, locator, "");
}
--------------------------------------------------------------------------------------
<2> Wait for all elements present in Dom, may not visisble
/**
* Wait for elements present in DOM, may not visible
* @param driver
* @param locator
* @param errorMessage
* @return
*/
1).
public static List<WebElement> waitForAllElementsPresent(WebDriver driver,final By locator, String errorMessage{
Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver driver) {
return driver.findElements(locator);
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Elements: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator) {
return waitForAllElementsPresent(driver, locator, "");
}
--------------------------------------------------------------------------------------
<3> Wait for context element present in DOM, may not visible
/**
* Finding element from the context, then wait for element present in DOM, may not visible
* @param driver
* @param locator
* @param context
* @param errorMessage
* @return
*/
1).
public static WebElement waitForElementPresent(WebDriver driver,final By locator, final WebElement context, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return context.findElement(locator);
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static WebElement waitForElementPresent(WebDriver driver,final By locator, final WebElement context) {
return waitForElementPresent(driver, locator, context, "");
}
-------------------------------------------------------------------------
<4> Wait for all the target context elements present in DOM, may not visible
/**
* Finding elements from the context, then wait for all the elements present in DOM, may not visible
* @param driver
* @param locator
* @param context
* @param errorMessage
* @return
*/
1).
public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator,
final WebElement context, String errorMessage) {
Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver driver) {
try {
List<WebElement> els = context.findElements(locator);
if (els.size() > 0) {
return els;
}
return null;
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Elements: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator,
final WebElement context) {
return waitForAllElementsPresent(driver, locator, context, "");
}
--------------------------------------------------------------------------------
<5> Wait for element visible
/**
* Wait for element visible
*
* @param driver
* @param locator
* @param errorMessage
* @return
*/
1).
public static WebElement waitForElementVisible(WebDriver driver, final By locator, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement el = driver.findElement(locator);
if (el.isDisplayed()) {
return el;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static WebElement waitForElementVisible(WebDriver driver,final By locator) {
return waitForElementVisible(driver, locator, "");
}
--------------------------------------------------------------------------------
<6> Wait for context element visible
/**
* Finding element from the context, wait for element visible
* @param driver
* @param locator
* @param context
* @param errorMessage
* @return
*/
1).
public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement el = context.findElement(locator);
if (el.isDisplayed()) {
return el;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
}
2).
public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context) {
return waitForElementVisible(driver, locator, context, "");
}
---------------------------------------------------------------------------------------------------------
<7> Wait for all the elements visible
/**
* Wait for all the elements visible
*
* @param driver
* @param locator
* @param errorMessage
* @return
*/
1).
public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator,String errorMessage{
Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver driver) {
try {
List<WebElement> els = driver.findElements(locator);
int count = 0;
for (WebElement el : els) {
if (el.isDisplayed()) {
count++;
}
}
if (count == els.size() && els.size() > 0) {
return els;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Elements: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator) {
return waitForAllElementsVisible(driver,locator,"");
}
-----------------------------------------------------------------------------------
<8> Wait for all the target context elements visible
/**
* Finding elements from the context, then wait for all the elements present visible
* @param driver
* @param locator
* @param context
* @param errorMessage
* @return
*/
1).
public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator,
final WebElement context,String errorMessage) {
Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver driver) {
try {
List<WebElement> els = context.findElements(locator);
int count = 0;
for (WebElement el : els) {
if (el.isDisplayed()) {
count++;
}
}
if (count == els.size() && els.size() > 0) {
return els;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Elements: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator,
final WebElement context){
return waitForAllElementsVisible(driver,locator,context,"");
} ----------------------------------------------------------------------------
1).
public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context, int seconds, String errorMessage) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement el = context.findElement(locator);
if (el.isDisplayed()) {
return el;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = createWait(driver);
if (errorMessage != "") {
wait.withMessage(errorMessage);
} else {
wait.withMessage("Cannot find Element: " + locator.toString());
}
return wait.until(waitFn);
} 2).
public static WebElement waitForElementVisible(WebDriver driver,final By locator, final WebElement context,
int seconds) {
return waitForElementVisible(driver, locator, context, seconds, "");
}
--------------------------------------------------------------------------------- 1)./**
* Check if element exists in DOM, maybe not visible
* @param driver
* @param locator
* @return
*/
public static Boolean isElementPresent(WebDriver driver, By locator) {
try {
driver.findElement(locator);
return true;
} catch (Exception e) {
return false;
}
} 2)./**
* Check if element exists from context node, maybe not visible
* @param context
* @param locator
* @return
*/
public static Boolean isElementPresent(WebElement context, By locator) {
try {
context.findElement(locator);
return true;
} catch (Exception e) {
return false;
}
} 3). /**
* Check if element is visible in DOM
* @param driver
* @param locator
* @return
*/
public static Boolean isElementVisible(WebDriver driver, By locator) {
try {
WebElement el = driver.findElement(locator);
if(el.isDisplayed()){
return true;
}
else{
return false;
}
} catch (Exception e) {
return false;
}
} 4). /**
* Check if element is visible from context node
* @param context
* @param locator
* @return
*/ public static Boolean isElementVisible(WebElement context, By locator) {
try {
WebElement el = context.findElement(locator);
if(el.isDisplayed()){
return true;
}
else{
return false;
}
} catch (Exception e) {
return false;
}
}
Automation 的 Wait 工具的更多相关文章
- 使用UI Automation实现自动化测试 --工具使用
当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...
- UI Automation编程辅助工具Inspect的下载和使用
UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序. In ...
- UIAutomation识别UI元素
MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词 ...
- Selenium常用API详解介绍
转至元数据结尾 由 黄从建创建, 最后修改于一月 21, 2019 转至元数据起始 一.selenium元素定位 1.selenium定位方法 2.定位方法的用法 二.控制浏览器操作 1.控制 ...
- 一篇文全面了解DevOps:从概念、关键问题、兴起到实现需求
一篇文全面了解DevOps:从概念.关键问题.兴起到实现需求 转自:一篇文全面了解DevOps:从概念.关键问题.兴起到实现需求 2018-06-06 目前在国外,互联网巨头如Google.Faceb ...
- DevOps简介
DevOps 是一个完整的面向IT运维的工作流,以 IT 自动化以及持续集成(CI).持续部署(CD)为基础,来优化程式开发.测试.系统运维等所有环节. DevOps的概念 DevOps一词的来自于D ...
- 【转】DevOps的前世今生
转自:http://www.infoq.com/cn/news/2016/09/learn-devops-from-reports 目前在国外,互联网巨头如Google.Facebook.Amazon ...
- 使用Python实现基于图像识别的iOS自动化测试
相对于Android来说,iOS比较封闭.这一点,在设计和评估自动化测试方案的时候感觉尤其强烈.iOS平台上没有特别好用的自动化测试工具.苹果针对iOS提供了UI Automation的Instrum ...
- Jenkins学习总结(6)——了解DevOps的前世今生
DevOps是什么?从哪里来? DevOps的概念 DevOps一词的来自于Development和Operations的组合,突出重视软件开发人员和运维人员的沟通合作,通过自动化流程来使得软件构建. ...
随机推荐
- 洛谷 P3332 [ZJOI2013]K大数查询 || bzoj3110
用树套树就很麻烦,用整体二分就成了裸题.... 错误: 1.尝试线段树套平衡树,码农,而且n*log^3(n)慢慢卡反正我觉得卡不过去 2.线段树pushdown写错...加法tag对于区间和的更新应 ...
- 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...
- ArrayList、HashMap 与 员工类(程序员、经理的结合使用) 相当于集合与继承的总结
package Day28ketangzuoye; import java.util.ArrayList; import java.util.HashMap; import java.util.Map ...
- 总结用CoreText绘制文本时遇到的问题以及解决办法
关于CoreText不做解释.用的人自然知道这个是干什么的. 功能非常强大,可以绘制文本,图片等. 这次用的Xcode7.0的版本.所以之前很多方法,现在不能用.也不是不能用,就是有黄色警告很不爽. ...
- Hibernate配置(外部配置文件方式)
配置Hibernate有2种方式,本文讲的是通过外部配置文件配置的方式 Hibernate核心配置文件 <?xml version='1.0' encoding='UTF-8'?> < ...
- php微信自动发红包
<?phpheader('Content-type:text');define("TOKEN", "weixin");$wechatObj = new w ...
- Abp Framework中文文档上线
感谢 ABP框架中国小组 给我们带来的ABP中文翻译,Web+为方便广大学习爱好者随时查阅,现推出了Gitbook风格的在线阅读文档:http://www.webplus.org.cn/documen ...
- 常用linux命令大全 转载自:https://www.cnblogs.com/laov/p/3541414.html(大牛笔记)
Linux简介及Ubuntu安装 Linux,免费开源,多用户多任务系统.基于Linux有多个版本的衍生.RedHat.Ubuntu.Debian 安装VMware或VirtualBox虚拟机.具体安 ...
- .vue文件在phpstorm中红线解决办法
主要原因是js版本太低, 1,安装vue.js插件, 2,设置file type,vue.js添加 *.vue, 3,切换js版本为es6,
- 归并排序算法及其JS实现
归并排序(MERGE-SORT)是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归求解,而治(con ...