自动化测试中,等待时间的运用占据了举足轻重的地位,平常我们需要处理很多和时间息息相关的场景,例如:
- 打开新页面,只要特定元素出现而不用等待页面全部加载完成就对其进行操作
- 设置等待某元素出现的时间,超时则抛出异常
- 设置页面加载的时间
- …..
webdriver类中有三个和时间相关的方法:
1.pageLoadTimeout
2.setScriptTimeout
3.implicitlyWait
我们就从这里开始,慢慢揭开他神秘的面纱。
pageLoadTimeout
pageLoadTimeout方法用来设置页面完全加载的超时时间,完全加载即页面全部渲染,异步同步脚本都执行完成。前面的文章都是使用get 方法登录安居客网站,大家应该能感觉到每次打开网页后要等很长一段时间才会进行下一步的操作,那是因为没有设置超时时间而get方法默认是等待页面全部加 载完成才会进入下一步骤,加入将超时时间设置为3S就会中断操作抛出异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importorg.openqa.selenium.WebElement;
publicclassNewTest{
publicstaticvoidmain(String[]args)throwsInterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver=newChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium");
}catch(Exceptione){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}
|
ps:如果时间参数为负数,效果没有使用这个方法是一样的,接口注释中有相关说明:”If the timeout is negative, page loads can be indefinite”.
如果想在抛出异常后并不中断而是继续执行下面的操作那该怎么办呢?可以使用try,catch的组合来实现这个功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importorg.openqa.selenium.WebElement;
publicclassNewTest{
publicstaticvoidmain(String[]args)throwsInterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver=newChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exceptione){
}finally{
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
if(input.isDisplayed())
input.sendKeys("selenium");
}
}
|
这样,当页面加载3S后就会执行下面的操作了。
setScriptTimeout
设置异步脚本的超时时间,用法同pageLoadTimeout一样就不再写了,异步脚本也就是有async属性的JS脚本,可以在页面解析的同时执行。
implicitlyWait
识别对象的超时时间,如果在设置的时间类没有找到就抛出一个NoSuchElement异常,用法参数也是和pageLoadTimeout一样,大家可以自己试验试验。
上文中介绍了如何才能在使用pageLoadTimeout抛出异常的同时继续执行,但是现有的方法还是不完美,如果输入框在3S后没有出现还是会 报错,怎么办呢?机智的同学可以想到用sleep方法来实现,为了方便演示换个更明显的需求来说明。安居客的首页上有个切换城市的链接,当点击城市的时候 就会显示全部的城市以供选择这时display属性就会变化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.interactions.Actions;
publicclassNewTest{
publicstaticvoidmain(String[]args)throwsInterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver=newChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exceptione){
}finally{
WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));
WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']"));
Actions actions=newActions(driver);
actions.clickAndHold(city).perform();
while(citys.isDisplayed()){
System.out.println("sleep");
Thread.sleep(3000);
}
Thread.sleep(3000);
driver.quit();
}
}
|
执行后会每过3S就会输出一次sleep,但是这样的代码显然不够高大上,而且没有限制会一直无限的等待下去,下面介绍一种高大上的方法,就是until,先看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.interactions.Actions;
importorg.openqa.selenium.support.ui.WebDriverWait;
importorg.openqa.selenium.support.ui.ExpectedCondition;
publicclassNewTest{
publicstaticvoidmain(String[]args)throwsInterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver=newChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exceptione){
}finally{
WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));
Actions actions=newActions(driver);
actions.clickAndHold(city).perform();
//最多等待10S,每2S检查一次
WebDriverWait wait=newWebDriverWait(driver,10,2000);
wait.until(newExpectedCondition<Boolean>(){
publicBooleanapply(WebDriver driver){
System.out.println("sleep");
return!driver.findElement(By.xpath("//div[@id='city-panel']")).isDisplayed();
}
});
Thread.sleep(3000);
driver.quit();
}
|
转载:http://www.360doc.com/content/14/0913/10/13497042_409106015.shtml
相关推荐
本书是一本从入门到精通模式的Selenium WebDriver实战经验分享书籍。全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用...
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
关于Selenium WebDriver工作原理的介绍,原理相关描述
selenium RC与selenium webdriver的区别
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
seleniumwebdriver(python) (第三版)
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
selenium webdriver基于python源码案例,全是案例适合小白入门学习
selenium2.0自动化
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
自动化测试:Selenium webdriver学习笔记C#版
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
Solve your Selenium WebDriver problems with this quick guide to automated testing of web applications with Selenium WebDriver in C#. Selenium WebDriver Recipes in C#, Second Edition contains hundreds ...
selenium webdriver基于python源码案例,全是案例适合小白入门学习
selenium webdriver2 环境搭建