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&lt;WebDriver&gt; wait = new FluentWait&lt;WebDriver&gt;(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function&lt;WebDriver, WebElement&gt;() {
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类的更多相关文章

  1. 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)

    1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...

  2. Selenium Webdriver——Table类封装

    WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...

  3. selenium - webdriver - Keys类(键盘操作)

    Keys()类提供了键盘上几乎所有按键的方法,这个类可用来模拟键盘上的按键,包括各种组合键,如 Ctrl+A, Ctrl+X,Ctrl+C, Ctrl+V 等等 from selenium impor ...

  4. selenium - webdriver - ActionChains类(鼠标操作)

    ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...

  5. Python+Selenium框架 ---一个类文件多个测试方法情况下测试固件的写法

    我们测试中,肯定需要,打开一个页面,然后测试这个页面的多个用例,才关闭这个页面,去测试其他页面,在unittest是有相关测试固件方法去支持这种行为.请看下面 # coding=utf-8 impor ...

  6. Selenium之ActionChains类、Keys类

    ActionChains类(鼠标操作)常用于模拟鼠标的行为,比如单击.双击.拖拽等行为. 一些常用的模拟鼠标的操作方法有: click(on_element=None)     --- 鼠标单击 do ...

  7. Selenium常见报错问题(1)- 先来认识下selenium常见异常类

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  8. selenium模块及类组织关系

    问题:webdriver子模块中为什么可以直接使用类Chrome.ChromeOptions.Firefox.FirefoxProfile... 在webdriver的__init__.py文件中已经 ...

  9. Selenium 的基础框架类

    个人写的一个selenium的base类,应该所有使用selenium的同事都会使用到: package com.hx.baserunner; import static java.io.File.s ...

随机推荐

  1. Winform调用QQ发信息并且开机启动 (开源)

    前言 公司CS系统需要加入启动qq从winform调用qq聊天窗口的功能,前提是需要将聊天者的QQ号码作为参数传递到函数中,一直没有搞过,正好很感兴趣,就折腾,Winform调用qq,我想肯定是需要一 ...

  2. 常用SQL查询语句

    一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指定列 SQL>SELECT empmo, ...

  3. Docker源码编译

    官方建议docker源码编译在docker容器内进行,因为官方提供的容器内已经继承了编译需要的环境,如果非要自己搭建编译环境也不是不可以,就是稍微有些繁琐.以下以1.8.2版本为例. 1.pull d ...

  4. mybatis mapper association collection

    1.Question Description: sometimes, POJO bean contains another bean or collection as property, it's s ...

  5. 【原创】使用.NET Core 1.0创建一个Self-Contained控制台应用

    开发机器:win7-x64 .NET Core版本:1.0.0-preview2-003121 Visual Studio Code:1.2.1 至于什么是Self-Contained应用类型以及与P ...

  6. ElasticSearch文档-简单介绍

    ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...

  7. request.getRequestDispatcher()的两个方法forward()/include()!!!

    RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");请求转发: rd.forward( reques ...

  8. 滴滴DSRC抢楼大赛,十一快车券飞起来

    十一马上就要到了 不管你想要去哪里 总少不了滴滴打车 DSRC为你准备了30份快车券 现在开始参与抢楼,就有机会得到 活动时间: 本帖发布之时起-9月30日12:00 参与方式: 关注微信公众号“滴滴 ...

  9. Oracle行列转换

    一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...

  10. iOS之UI--关于modal

    modal的效果展示: 关于modal的涉及到的知识点: 1.当一个控制器被销毁的时候,它里面所有子控制器的业务逻辑都不能够处理 2.当一个控制器被销毁的时候,它里面所有子控件的业务逻辑都不能够处理 ...