selenium之ExpectedConditions类
API中对于该类的介绍:Canned ExpectedConditions which are generally useful within webdriver tests。很笼统,大概意思就是在webdriver的测试中会有用,那到底有什么用呢,下面我们就来一探究竟。
该类没有构造函数,所有的方法都是静态的,所以我们可以直接用类名调用,我只介绍里面几个方法,其它方法的用法都类似,具体查api
- 1.alertIsPresent()判断alert弹框出现了没,返回值是ExpectedCondition<Alert>,我们可以这样用
public void testalertIsPresent() { dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
Alert alert = e.apply(dr);
alert.accept();
}也可以这样用
public void testalertIsPresentW() {
WebDriverWait wait = new WebDriverWait(dr,10);
dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
Alert alert = wait.until(e);
alert.accept();
}apply用了com.google.common.base.Function的方法,而until用了FluentWait的方法。那么这两者有和区别呢,通过下面这个例子介绍,
- 2. textToBePresentInElementLocated():文本是否出现在所定位的元素中
public void testtextToBePresentInElementLocated() {
WebDriverWait wait = new WebDriverWait(dr,10);
dr.findElement(By.className("wait")).click();
ExpectedCondition<Boolean> e1 = ExpectedConditions.textToBePresentInElementLocated(By.className("red"),"wait for display");
System.out.println(e1.apply(dr));//false
//System.out.println(wait.until(e1)); //报true }apply不会去等待所找元素是否存在,直接false报错no such elment。而until会在等待10秒,如果出现则返回true,否则超时。
3.visibilityOfElementLocated(),该方法是判断定位的元素是否存在,使用的方法时(),apply也会去等待
public void testvisibilityOfElementLocated() {
WebDriverWait wait = new WebDriverWait(dr,10);
dr.findElement(By.className("wait")).click();
ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red")); System.out.println(wait.until(e1).getText()); //wait for display
System.out.println(e1.apply(dr).getText()); //wait for display }4.visibilityOf(),判断元素是否可见
public void testvisibilityOf() {
WebDriverWait wait = new WebDriverWait(dr,10);
ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOf(dr.findElement(By.className("wait"))); System.out.println(e1.apply(dr).getAttribute("value")); //wait
//System.out.println(wait.until(e1).getAttribute("value"));//wait
}
5.visibilityOfElementLocated(),定位的元素是否可见
public void testvisibilityOfElementLocated() {
WebDriverWait wait = new WebDriverWait(dr,10);
dr.findElement(By.className("wait")).click();
ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red"));
System.out.println(wait.until(e1).getText()); //wait for display
System.out.println(e1.apply(dr).getText()); //wait for display
}
以上列举了TestExpectedConditions的用法,鉴于apply会存在失败的情况,建议使用until。
PS:小插曲
下面的例子是api中关于FluentWait的用法
Sample usage: // Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
详细用法:
@Test(enabled=false)
public void testuntilWebElement() {
WebDriverWait wait = new WebDriverWait(dr,10);
WebElement we = wait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver arg0) {
// TODO Auto-generated method stub
return arg0.findElement(By.id("user"));
}}) ;
we.sendKeys("test");
} @Test(enabled=false)
public void testuntilBoolean() {
WebDriverWait wait = new WebDriverWait(dr,10);
boolean we = wait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver arg0) {
// TODO Auto-generated method stub
return arg0.findElement(By.id("user"));
}}).isDisplayed();
System.out.println(we);
}
两者用法目的差不多,可以根据具体情况选择使用哪种方式。
文中所用到的html例子:
http://pan.baidu.com/s/1i3jjo3b
selenium之ExpectedConditions类的更多相关文章
- 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)
1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...
- Selenium Webdriver——Table类封装
WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...
- selenium - webdriver - Keys类(键盘操作)
Keys()类提供了键盘上几乎所有按键的方法,这个类可用来模拟键盘上的按键,包括各种组合键,如 Ctrl+A, Ctrl+X,Ctrl+C, Ctrl+V 等等 from selenium impor ...
- selenium - webdriver - ActionChains类(鼠标操作)
ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...
- Python+Selenium框架 ---一个类文件多个测试方法情况下测试固件的写法
我们测试中,肯定需要,打开一个页面,然后测试这个页面的多个用例,才关闭这个页面,去测试其他页面,在unittest是有相关测试固件方法去支持这种行为.请看下面 # coding=utf-8 impor ...
- Selenium之ActionChains类、Keys类
ActionChains类(鼠标操作)常用于模拟鼠标的行为,比如单击.双击.拖拽等行为. 一些常用的模拟鼠标的操作方法有: click(on_element=None) --- 鼠标单击 do ...
- Selenium常见报错问题(1)- 先来认识下selenium常见异常类
如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...
- selenium模块及类组织关系
问题:webdriver子模块中为什么可以直接使用类Chrome.ChromeOptions.Firefox.FirefoxProfile... 在webdriver的__init__.py文件中已经 ...
- Selenium 的基础框架类
个人写的一个selenium的base类,应该所有使用selenium的同事都会使用到: package com.hx.baserunner; import static java.io.File.s ...
随机推荐
- 使用Dhcpstarv解决DHCP服务器冲突问题
场景: 内网环境需要开启多个DHCP服务器,分别给不同的设备进行PXE安装. 存在的问题: 多个DHCP的情况下,设备在启动时随机从一个DHCP服务器获取IP(哪个DHCP服务器先响应就从哪个获取)并 ...
- platform总线globalfifo驱动
功能是使用内存的4k单元,实现读,写,偏移,清除. /************************************************************************* ...
- 内核移植和文件系统制作(3)Ramdisk简介和常见问题
一,Ramdisk简介: Ramdisk是一种基于内存的虚拟文件系统(并非一个实际的文件系统),它将一部分固定大小(这个大小在编译内核的make menuconfig时配置)的内存当作硬盘一个分区来使 ...
- treap树---营业额统计
台州学院 2924 描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况.Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...
- Treap树的基础知识
原文 其它较好的的介绍:堆排序 AVL树 树堆,在数据结构中也称Treap(事实上在国内OI界常称为Traep,与之同理的还有"Tarjan神犇发明的"Spaly),是指有一个随 ...
- 1秒30000QPS,前后端设计思路
Q:现在有这样一个需求,在一秒中有3万的支付订单请求,有什么比较好的解决方案吗? PS:我们数据库用的是oracle 程序是java spring mybatis dubbo mq等技术,现在有这样一 ...
- 写给java程序员的c++与java实现的一些重要细微差别
0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...
- 误报的java.sql.SQLException: Parameter number 21 is not an OUT parameter
今天为了模拟一个mysql内存不释放问题,要测试一个存储过程,同时具有出参和入参,启动时报了上述错误. <select id="funcl_trd_secu_execution_que ...
- django性能优化
1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...
- Xml序列化、反序列化帮助类
之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 /// <summary> / ...