基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待
一、隐式等待
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类自带的等待方法:
最常用的是第三个,判断元素在页面中是否存在: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); //打印文本值
}
}
自定义显式等待,要注意:
期望值是WebElement类型,那么返回值一定也是WebElement类型;
我们专注于持续集成,更多原创请关注:www.hordehome.com
基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待的更多相关文章
- 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...
- 基于Selenium2+Java的UI自动化(1) - 原理和环境搭建
一.Selenium2的原理 Selenium1是thoughtworks公司的一个产品经理,为了解决重复烦躁的验收工作,写的一个自动化测试工具,其原理是用JS注入的方 式来模拟人工的操作,但是由于J ...
- 基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框
alert.confirm.prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理.而现在webdriver对这些弹出框做了专门的处理,使用seleni ...
- 基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本
一.操作日期选择框 QQ图片20161118215530.png1336x545 22.6 KB 说明:日期选择框大部分是不支持前端输入的,因为这个对象是 readOnly,只读属性,selenium ...
- 基于Selenium2+Java的UI自动化(2) - 启动浏览器
一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...
- 基于Selenium2+Java的UI自动化(3) - 页面元素定位
一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
- Java并发之显式锁和隐式锁的区别
Java并发之显式锁和隐式锁的区别 在面试的过程中有可能会问到:在Java并发编程中,锁有两种实现:使用隐式锁和使用显示锁分别是什么?两者的区别是什么?所谓的显式锁和隐式锁的区别也就是说说Synchr ...
- selenium-webdriver中的显式等待与隐式等待
在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...
随机推荐
- Firebug控制台详解,让调试js代码变得更简单
http://www.open-open.com/lib/view/open1373120100347.html Firebug是网页开发的利器,能够极大地提升工作效率. Firebug控制台详解 控 ...
- docker的资源限制cpuset cpuquota memory
总结 目前,公司7u已经不再使用lxc,转而使用libcontainer 即native docker对cpuquota的支持目前是有问题的,一般大家使用docker的时候,主要是对memory,cp ...
- Java Ant Could not find the main class: org.eclipse.ant.internal.launching.remote.InternalAntRunner. Program
参考:http://blog.csdn.net/jiangtaoking/article/details/49151763 The solution is to go to Run as → Exte ...
- jQuery replaceWith replaceAll end的用法
jQuery replaceWith replaceAll end的用法 <%@ page language="java" import="java.util.*& ...
- Error while registering Oracle JDBC Diagnosabilityh
Error while registering Oracle JDBC Diagnosability 把ojdbc6.jar换成ojdbc14.jar就可以了: 后来发现又没有问题了:不过据说这个是数 ...
- 选择法排序 vb.net
Imports System.ThreadingModule Module1 Sub Main() 'test code 'Dim a, b As Integer ...
- 从svn上回滚版本
转载地址:http://blog.csdn.net/happyqyt/article/details/7107039 提交SVN后想回滚到旧版本. 选择TortoiseSVN→Repo-browser ...
- 【js 编程艺术】小制作三
1.html文件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- Hadoop学习笔记-003-CentOS_6.5_64_设置ssh免密码登录
参考:http://blog.csdn.net/u010270403/article/details/51444677 虚拟机中共五个centos系统,每个系统有两个用户root和hadoop:cdh ...
- KB奇遇记(10):终章
本来还想写一篇关于前CIO的著名言论,不过想想还是算了.博客空间宝贵,不乱恶心人了. 这篇博文是本系列<KB奇遇记>的最后一篇了. 虽然在KB公司有这么多的苦,但毕竟收获也很多,至少让我懂 ...