selenium webdriver—超时设置

selenium webdriver—超时设置 博客分类: java  

自动化测试中,等待时间的运用占据了举足轻重的地位,平常我们需要处理很多和时间息息相关的场景,例如:

  • 打开新页面,只要特定元素出现而不用等待页面全部加载完成就对其进行操作
  • 设置等待某元素出现的时间,超时则抛出异常
  • 设置页面加载的时间
  • …..

webdriver类中有三个和时间相关的方法:
  1.pageLoadTimeout
  2.setScriptTimeout
  3.implicitlyWait

我们就从这里开始,慢慢揭开他神秘的面纱。

 pageLoadTimeout

pageLoadTimeout方法用来设置页面完全加载的超时时间,完全加载即页面全部渲染,异步同步脚本都执行完成。前面的文章都是使用get 方法登录安居客网站,大家应该能感觉到每次打开网页后要等很长一段时间才会进行下一步的操作,那是因为没有设置超时时间而get方法默认是等待页面全部加 载完成才会进入下一步骤,加入将超时时间设置为3S就会中断操作抛出异常

ps:如果时间参数为负数,效果没有使用这个方法是一样的,接口注释中有相关说明:”If the timeout is negative, page loads can be indefinite”.

如果想在抛出异常后并不中断而是继续执行下面的操作那该怎么办呢?可以使用try,catch的组合来实现这个功能

这样,当页面加载3S后就会执行下面的操作了。

 setScriptTimeout

设置异步脚本的超时时间,用法同pageLoadTimeout一样就不再写了,异步脚本也就是有async属性的JS脚本,可以在页面解析的同时执行。

 implicitlyWait

识别对象的超时时间,如果在设置的时间类没有找到就抛出一个NoSuchElement异常,用法参数也是和pageLoadTimeout一样,大家可以自己试验试验。

上文中介绍了如何才能在使用pageLoadTimeout抛出异常的同时继续执行,但是现有的方法还是不完美,如果输入框在3S后没有出现还是会 报错,怎么办呢?机智的同学可以想到用sleep方法来实现,为了方便演示换个更明显的需求来说明。安居客的首页上有个切换城市的链接,当点击城市的时候 就会显示全部的城市以供选择这时display属性就会变化

1

执行后会每过3S就会输出一次sleep,但是这样的代码显然不够高大上,而且没有限制会一直无限的等待下去,下面介绍一种高大上的方法,就是until,先看代码

Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedCondition; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ }finally{ WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']")); Actions actions=new Actions(driver); actions.clickAndHold(city).perform(); //最多等待10S,每2S检查一次 WebDriverWait wait=new WebDriverWait(driver,10,2000); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { System.out.println("sleep"); return !driver.findElement(By.xpath("//div[@id='city-panel']")).isDisplayed(); } }); Thread.sleep(3000); driver.quit(); } }
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
import org . openqa . selenium . By ;
import org . openqa . selenium . WebDriver ;
import org . openqa . selenium . chrome . ChromeDriver ;
import org . openqa . selenium . WebElement ;
import org . openqa . selenium . interactions . Actions ;
import org . openqa . selenium . support . ui . WebDriverWait ;
import org . openqa . selenium . support . ui . ExpectedCondition ;
 
public class NewTest {
     public static void main ( String [ ] args ) throws InterruptedException {
         System . setProperty ( "webdriver.chrome.driver" ,
                 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ) ;
         WebDriver driver = new ChromeDriver ( ) ;
         try {
             //设置超时时间为3S
             driver . manage ( ) . timeouts ( ) . pageLoadTimeout ( 3 , TimeUnit . SECONDS ) ;
             driver . get ( "http://shanghai.anjuke.com" ) ;
         } catch ( Exception e ) {
            
         } finally {
             WebElement city = driver . findElement ( By . xpath ( "//a[@id='switch_apf_id_8']" ) ) ;
             Actions actions = new Actions ( driver ) ;
             actions . clickAndHold ( city ) . perform ( ) ;
            
             //最多等待10S,每2S检查一次
             WebDriverWait wait = new WebDriverWait ( driver , 10 , 2000 ) ;
            
             wait . until ( new ExpectedCondition <Boolean> ( ) {
                 public Boolean apply ( 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

转载于:https://my.oschina.net/xiaominmin/blog/1599630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值