【slenium专题】Webdriver同步设置

系统 1737 0

Webdriver同步设置常用等待类主要如下图所示

【slenium专题】Webdriver同步设置

 

注:support.ui包内类主要实现显性等待功能,timeouts()内方法主要实现隐性等待功能

一.线程休眠 

      Thread.sleep(
      
        long
      
       millis)
    

 

二.隐形等待

  隐性等待:设置一次,driver整个生命周期中都在使用,不需要针对元素明确设置

      driver.manage().timeouts().implicitlyWait(
      
        long
      
       outTime, TimeUnit unit);
    

全局设置,设置driver执行所有定位元素操作的超时时间

Parameters: outTime – 超时等待时间; unit – 时间单位.

Ex. 

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
        //
      
      
        超时等待时间为10S
      
    

 

 

 

三.显性等待

 

    显性等待:需要等待的元素定位时需要逐一明确调用该等待方法

【slenium专题】Webdriver同步设置

Figure 1 Support.ui包常用类UML

 

[ 常用等待模板 ]

  1. 等待元素加载

    1)WebDriverWait

      
        new
      
       WebDriverWait(WebDriver driver, 
      
        long
      
      
         timeOutInSeconds).until(ExpectedConditions.presenceOfElementLocated(By by))
      
    

注解

  • Parameters driver-webdriver; timeOutInSeconds- 超时 等待时间( s ; by- 元素 locator
  • timeOutInSeconds 范围内,等待满足 until() 方法中的条件 ,即刻跳出等待。
  • 超出 timeOutInSeconds 抛出异常.

  2)FluentWait  

      Wait<WebDriver> wait = 
      
        new
      
       FluentWait<WebDriver>
      
        (driver)



             .withTimeout(
      
      
        long
      
      
         timeOut, TimeUnit unit)



             .pollingEvery(
      
      
        long
      
      
         duration, TimeUnit unit)



             .ignoring(exceptionType);



wait.until(ExpectedConditions.presenceOfElementLocated(By.by));
      
    

注解

  • Parameters driver-webdriver; timeOut - 超时等待时间( s ; unit-时间单位; by- 元素 locator;

    duration-查找元素时间间隔 ; exceptionType-忽略异常类型,例如 默认NoSuchElementException.class

  • timeOut 范围内, driver 每隔 dration 定位一次元素,若遇到 exceptionType 则继续等待,直到满足 until() 方法中的条件 ,即刻跳出等待。
  • 超出 timeOutInSeconds 未满足until条件 抛出异常.

  2.等待并获取元素—仅需要修改 until 方法体

 

      WebElement element =
      
         wait.until(

  
      
      
        new
      
       ExpectedCondition<WebElement>
      
        (){

    @Override

    
      
      
        public
      
      
         WebElement apply( WebDriver driver) {

        
      
      
        return
      
      
         driver.findElement( By by);

    }

  }

);
      
    

注解

  • Parameters driver-webdriver W ebDriverWait|FluentWait 实例 化方法中获取 ; by- 元素 locator
  • Return: 若定位到元素,返回webelement;否则报异常

[ 案例 ]

  1. 等待页面元素加载
    • WebDriverWait
      Wait<WebDriver> wait = 
      
        new
      
       WebDriverWait(driver,10
      
        );



wait.until(

    ExpectedConditions. presenceOfElementLocated(By.id(
      
      "myDynamicElement"
      
        ))

);
      
    

方法功能:定位 id=' myDynamicElement' 的元素,超时等待时间为 10S

    • FluentWait
      Wait<WebDriver> wait = 
      
        new
      
       FluentWait<WebDriver>
      
        (driver)



             .withTimeout(
      
      60
      
        , TimeUnit.SECONDS)



             .pollingEvery(
      
      10
      
        , TimeUnit.SECONDS)



             .ignoring(NoSuchElementException.
      
      
        class
      
      
        );



wait.until(ExpectedConditions.presenceOfElementLocated(By.id(
      
      "myDynamicElement")));
    

方法功能:定位 id=' myDynamicElement' 的元素,超时等待时间为 60S ,每隔 10S 定位一次,遇到 NoSuchElementException 报错忽略,继续等待直到超过 60s

  1. 等待并获取页面元素
    • WebDriverWait
      Wait<WebDriver> wait = 
      
        new
      
       WebDriverWait(driver, 10
      
        );



WebElement e 
      
      =
      
         wait.until(

    
      
      
        new
      
       ExpectedCondition< WebElement>
      
        (){



        @Override

        
      
      
        public
      
      
         WebElement apply( WebDriver d) {

            
      
      
        return
      
       d.findElement( By.id( " myDynamicElement "
      
         ));

        }

    }

);    
      
    

方法功能:定位 id=' myDynamicElement' 的元素,超时等待时间为 10S ,若定位到元素,直接返回元素

    •   FluentWait
      Wait<WebDriver> wait = 
      
        new
      
       FluentWait<WebDriver>
      
        (driver)

             .withTimeout(
      
      60
      
        , TimeUnit.SECONDS)

             .pollingEvery(
      
      10
      
        , TimeUnit.SECONDS)

             .ignoring(NoSuchElementException.
      
      
        class
      
      
        );

 

WebElement element
      
      =
      
         wait.until(

        
      
      
        new
      
       ExpectedCondition<WebElement>
      
        () {

            @Override

            
      
      
        public
      
      
         WebElement apply( WebDriver d) {

                
      
      
        return
      
       d.findElement( By.id( " myDynamicElement "
      
         ));

            }

        }

);
      
    

方法功能:定位 id=' myDynamicElement' 的元素,超时等待时间为 60S ,每隔 10S 定位一次, 60s 内遇到 NoSuchElementException 报错忽略,继续等待;若定位到元素,直接返回该页面元素。

 

如果只是仅仅想判断页面是不是加载到某个地方了 , 就可以用第一种方法 ; 但如果需要得到某个 WebElement, 两种方式都可以 , 只是第一种方式还需要再多一步获取的操作 .

四.Wait

FluentWait类是Wait接口的实现,直接使用wait接口可以更灵活的完成您需要的等待功能,例如

      Wait w = 
      
        new
      
      
         Wait(){



    @Override



    
      
      
        public
      
      
        boolean
      
      
         until() {



    
      
      
        return
      
      
         webElement.isDisplayed();



    }

};    
      
    

这种等待的方式 , 据说在加载 js 代码的时候做判断会比较方便 , 目前没有调试成功。

 五、其他

1.等待页面加载

      driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
    

注解:

  • pageloadTimeout方法只适用于firefox浏览器,Chrome等其他浏览器并不适用,但我们可以自己实现一个定时器
  • 该方法设置页面加载超时时间为100s

 

2.等待异步脚本加载

 

      driver.manage().timeouts().setScriptTimeout(100,SECONDS);
    

尚未调试通过

【slenium专题】Webdriver同步设置


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论