一、隐式等待


package com.automation.waits;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; /**
* 类说明:隐式等待
* <br/>
* @version 1.0
* 2016年11月22日 下午8:56:14
*/
public class ImplictWait { public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
/**
* 3.设置全局隐式等待时间;
* <br/>使用implicitlyWait方法,设定查找元素的等待时间;
* <br/>当调用findElement方法的时候,没有立刻找到元素,就会按照设定的隐式等待时长等待下去;
* <br/>如果超过了设定的等待时间,还没有找到元素,就抛出NoSuchElementException异常;
*/
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //设定隐式等待时间为10秒;
//3.打开搜狗首页;
driver.get("http://www.sogou.com/"); try {
//4.定位搜狗首页:输入框对象、搜索按钮;
WebElement inputBox = driver.findElement(By.id("query"));
WebElement searchButton = driver.findElement(By.id("stb")); //5.在输入框中输入元素,然后点击搜索按钮。
inputBox.sendKeys("输入框元素被找到了。");
searchButton.click(); } catch (NoSuchElementException e) { //如果元素没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
}
}
}

隐式等待可以设定,但是有一个缺点:
缺点:如果我们在代码中设定了隐式等待时间,当使用driver.findElement(By.*)

方法去查找页面元素的时候,如果没有第一时间找到元素,程序会等待下去。例如设置了隐式等待时间为10秒,某个元素没有一开始就出现,而是在第5秒的时候
出现了,程序依然会等待10秒,然后才向下执行;
所以,推荐使用显示等待。

二、显式等待


显示等待比隐式等待,更加节约测试执行的时间;
优势:
1. 等待的方法多,都是ExpectedConditions类中的自带方法,可以进行不同的等待判断;
2. 等待灵活,等设置显示等待10秒,而在第5秒元素出现了,那么就会立即向下执行,而不会继续等待;只有超过了10秒,才抛出NoSuchElementException异常;


ExpectedConditions类自带的等待方法:


粘贴的图像816x232 3.37 KB

最常用的是第三个,判断元素在页面中是否存在:presenceOfElementLocated(By locator)


package com.automation.waits;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* 类说明:显示等待
* <br/>
* @version 1.0
* 2016年11月22日 下午9:38:12
*/
public class explictWait { public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
//3.打开搜狗首页;
driver.get("http://www.baidu.com/"); /*
* 4.设置显示等待时长:10秒;
*/
WebDriverWait wait = new WebDriverWait(driver, 10); //5.显示等待:标题是否出现:
try {
wait.until(ExpectedConditions.titleContains("百度一下,你就知道"));
System.out.println("百度首页的标题出现了:百度一下,你就知道");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的标题没有找到");
} //6.显示等待:搜索框是否出现-------最常用的显示等待;
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
System.out.println("百度首页的搜索输入框出现了");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的输入框没有找到");
} //7.显示等待:页面搜索按钮是否可以被点击;
try {
wait.until(ExpectedConditions.elementToBeClickable(By.id("kw")));
System.out.println("百度首页的搜索按钮可以被点击");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的搜索按钮找不到");
}
}
}

三、自定义显式等待

package com.automation.waits;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* 类说明:自定义显示等待
* <br/>
* @version 1.0
* 2016年11月22日 下午10:00:12
*/
public class CustomExplictWait { public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
//3.打开搜狗首页;
driver.get("http://www.baidu.com/"); /*
* 4.自定义显示等待,在等待代码中找到某个元素;
*/
WebElement textInputBox =
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver){
return driver.findElement(By.xpath("//*[@type='text']"));
}
});
textInputBox.sendKeys("自定义显式等待1"); /*
* 5.自定义显示等待,获取页面元素//a[text()='关于搜狗']的文本值
*/
String text =
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<String>() {
@Override
public String apply(WebDriver driver){
return driver.findElement(By.xpath("//a[text()='关于搜狗']")).getText();
}
});
System.out.println(text); //打印文本值
}
}

自定义显式等待,要注意:

粘贴的图像915x200 9.17 KB

期望值是WebElement类型,那么返回值一定也是WebElement类型;

我们专注于持续集成,更多原创请关注:www.hordehome.com

基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待的更多相关文章

  1. 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍

    1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...

  2. 基于Selenium2+Java的UI自动化(1) - 原理和环境搭建

    一.Selenium2的原理 Selenium1是thoughtworks公司的一个产品经理,为了解决重复烦躁的验收工作,写的一个自动化测试工具,其原理是用JS注入的方 式来模拟人工的操作,但是由于J ...

  3. 基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框

    alert.confirm.prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理.而现在webdriver对这些弹出框做了专门的处理,使用seleni ...

  4. 基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本

    一.操作日期选择框 QQ图片20161118215530.png1336x545 22.6 KB 说明:日期选择框大部分是不支持前端输入的,因为这个对象是 readOnly,只读属性,selenium ...

  5. 基于Selenium2+Java的UI自动化(2) - 启动浏览器

    一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...

  6. 基于Selenium2+Java的UI自动化(3) - 页面元素定位

    一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...

  7. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

  8. Java并发之显式锁和隐式锁的区别

    Java并发之显式锁和隐式锁的区别 在面试的过程中有可能会问到:在Java并发编程中,锁有两种实现:使用隐式锁和使用显示锁分别是什么?两者的区别是什么?所谓的显式锁和隐式锁的区别也就是说说Synchr ...

  9. selenium-webdriver中的显式等待与隐式等待

    在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...

随机推荐

  1. jQuery插件Flot实战Demo

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. js判断MAC地址

    function white_mac_FormCheck(mac)    {           var temp = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0- ...

  3. 如何用C语言封装 C++的类,在 C里面使用

    本文给出了一种方法.基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在  wrapper的实现文件里实现. 1. apple.h #ifnde ...

  4. xtemplate语法

    XTemplate 是富逻辑的 KISSY 模板引擎,面向复杂的业务逻辑场景,同时保持高性能和丰富的配置方法,是易学易懂的模板语言. 一个典型的XTemplate模板实例: Hello {{name} ...

  5. UNIX网络编程中的需要注意的问题

    字节流套接字上调用read或write,输入或输出的字节数可能比请求的数量少,这个现象的原因在于内核中用于套接字的缓冲区可能已经达到了极限.此时所需要的是调用者再次调用read或write函数.这个现 ...

  6. SimpleDateFormat解析的时候字符串过长问题

    竟然不会报错: try { SimpleDateFormat dateFormatFrom = new SimpleDateFormat("yyyyMMddHHmmss"); St ...

  7. Spring Boot启动过程(一)

    之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringAp ...

  8. Oracle-11g 基于 NBU 的 rman 冷备份及恢复

    html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", "微软雅 ...

  9. 为 Jenkins 配置 .Net 持续集成环境

    去年年底,得益于公司引入 Jenkins,让我们在持续集成方面迈出了第一步,本文不赘述如何安装 Jenkins,主要关注点在于配置 .Net 环境.另外本文是在 Windows 环境下安装的 Jenk ...

  10. 转:C# Process.Start()方法详解

    http://blog.csdn.net/czw2010/article/details/7896264 System.Diagnostics.Process.Start(); 能做什么呢?它主要有以 ...