zoukankan      html  css  js  c++  java
  • 显式等待-----Selenium快速入门(十)

      上一篇说了元素定位过程中的隐式等待,今天我们来探讨一下显示等待。显式等待,其实就是在使用WebDriverWait这个对象,进行等待。显式等待对比隐式等待,多了一些人性化的设置,可以说是更细化的隐式等待。

      WebDriverWait 类继承自泛型类 FluentWait<T> ,而这个泛型类,又是泛型接口 Wait<T> 的实现。Wait<T>这个泛型接口只有一个方法,就是until,这也是我们需要重点掌握的方法,而FluentWait<T>实现了until方法,而且还扩充了一些设置,例如,设置等待时间,每隔多少时间执行一次,执行过程中哪些异常可以忽略,以及超时的时候显示的信息。同样,WebDriverWait既然继承了FluentWait<T>,也会拥有一样的方法。

      首先,我们如何得到WebDriverWait的实例呢,他的构造函数有3个。

      1.WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) 

         driver:这个不用说,大家都应该知道。

         clock:Clock是一个接口,clock参数是用来定义超时,一般使用默认即可。关于更详细的Clock接口,可以点击这里

              sleeper:Sleeper也是一个接口,sleeper这个参数是,让当前线程进入睡眠的一个对象

         timeOutInSeconds:超时的时间,单位是秒

         sleepTimeOut:线程睡眠的时间,默认是500毫秒,也就是默认500毫秒执行一次

      2.WebDriverWait(WebDriver driver, long timeOutInSeconds)

           3.WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)

      有了第一个构造函数的解释,后面两个就很简单了,实际上,后面两个构造函数更常用。

      由于Clcok和Sleeper都是接口,我们可以自己重新实现,也可以用已有的实现,例如,SystemClock就实现了Clock,而Sleeper,他有一个static final的字段SYSTEM_SLEEPER,我们可以使用它来作为参数。下面是Sleeper接口的源码

    public interface Sleeper {
    
      public static final Sleeper SYSTEM_SLEEPER = new Sleeper() {
        public void sleep(Duration duration) throws InterruptedException {
          Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
        }
      };
    
      /**
       * Sleeps for the specified duration of time.
       *
       * @param duration How long to sleep.
       * @throws InterruptedException If the thread is interrupted while sleeping.
       */
      void sleep(Duration duration) throws InterruptedException;
    }

      所以,要使用构造函数一得到wait对象,可以这样

    WebDriverWait wait=new WebDriverWait(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, 10, 1000);

      实际上,当我们使用后面两个构造函数的时候,其实也是在默认使用这两个参数,我们看看WebDriverWait构造函数的源码

     /**
       * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
       * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
       * list by calling ignoring(exceptions to add).
       *
       * @param driver The WebDriver instance to pass to the expected conditions
       * @param timeOutInSeconds The timeout in seconds when an expectation is called
       * @param sleepInMillis The duration in milliseconds to sleep between polls.
       * @see WebDriverWait#ignoring(java.lang.Class)
       */
      public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis) {
        this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
      }

      懂得第一个构造函数的使用,后面两个就不啰嗦了,后面两个实际是在使用默认的参数调用第一个构造函数。

      现在我们得到了WebDriverWait对象,怎么实现等待呢。刚说了,until是当中最重要的一个方法,我们看看WebDriverWait中这个until方法的定义。

      public <V> V until(java.util.function.Function<? super WebDriver,V> isTrue) ,

      until方法中的参数是一个Function,其中? super WebDriver是参数,表示包括WebDriver在内的所有WebDriver的父类,V是返回值的类型。

      例如:

         //得到WebDriver
            WebDriver driver=DriverHelper.CreateChromeDriver();
            
            //定义超时10秒,默认每500毫秒轮询一次
            WebDriverWait wait=new WebDriverWait(driver,10);
            //重新定义轮询时间,每隔1秒轮询一次
            wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
            //忽略NoSuchElementException异常
            wait.ignoring(NoSuchElementException.class);
            
            //等待10秒,每隔1秒定位一次,直至超时或返回要找的元素
            WebElement ele = wait.until(new Function<WebDriver,WebElement>() {
                @Override
                public WebElement apply(WebDriver arg0) {
                    // TODO Auto-generated method stub
                    return arg0.findElement(By.id("eleID"));
                }
                
            });

      until的参数太复杂,搞不懂?没关系,官方早已定义了常见的条件。这些条件都在ExpectedConditions这个类中。里面的方法很多,看名字基本就能猜到用途,官方的文档为:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

      例如:

        //得到WebDriver
            WebDriver driver=DriverHelper.CreateChromeDriver();
            
            //定义超时10秒,默认每500毫秒轮询一次
            WebDriverWait wait=new WebDriverWait(driver,10);
           
            //等待直到标题包含abc
            wait.until(ExpectedConditions.titleContains("abc"));
            
            //等待直到元素可点击
            wait.until(ExpectedConditions.elementToBeClickable(By.id("aaa")));
            
            //等待直到url包含cnblogs.com
            wait.until(ExpectedConditions.urlContains("cnblogs.com"));

      更多的预置条件请查看:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

      下面通过一个例子,来看看具体怎么使用。

      首先,我们在html文件夹新建一个wait.html,html代码如下:

    <!DOCTYPE html>
    <html>
    <head> 
    <meta> 
    <title></title> 
    </head>
    <body>
    
    <input type="text" id="inputID" style="display:none"/>
    <input type="button" value="点击" onclick="document.getElementById('inputID').style.display='';"/>
    
    </body>
    </html>

      然后,我们的代码要实现的功能是,等待5秒,直到inputID这个文本框显示,我们就在文本框内输入“hello,selenium",html中有个按钮,就是用来控制文本框的显示隐藏状态。要实现这个功能,我们需要用到visibilityOfElementLocated(By located)。

      代码如下:

         //得到WebDriver
            WebDriver driver=DriverHelper.CreateChromeDriver();
            //跳转到html
            driver.get("file:///D:/WorkSpace/SeleniumTest/html/wait.html");
    
            //等待5秒,如果inputID显示,则输入"hello,selenium",否则,输出信息“定位元素超时”
            WebDriverWait wait=new WebDriverWait(driver, 5);
            try
            {
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("inputID"))).sendKeys("hello,selenium");
            }
            catch (org.openqa.selenium.TimeoutException e) 
            {
                System.out.print("定位元素超时!");
            }
           

      执行效果如下:

      

  • 相关阅读:
    logback配置和使用
    安装多个jdk导致eclipse打不开问题
    Spring事务管理
    使用JavaConfig配置SpringMVC
    Spring pom.xml配置
    Maven私服搭建(Nexus Repository Manager 3)
    Maven多环境配置
    Maven多模块构建实例
    Maven依赖机制
    Maven安装与配置
  • 原文地址:https://www.cnblogs.com/devtester/p/8648490.html
Copyright © 2011-2022 走看看