selenium之ExpectedConditions类

API中对于该类的介绍:Canned ExpectedConditions which are generally useful within webdriver tests。很笼统,大概意思就是在webdriver的测试中会有用,那到底有什么用呢,下面我们就来一探究竟。

该类没有构造函数,所有的方法都是静态的,所以我们可以直接用类名调用,我只介绍里面几个方法,其它方法的用法都类似,具体查api

 

  • 1.alertIsPresent()判断alert弹框出现了没,返回值是ExpectedCondition<Alert>,我们可以这样用
  •     public void testalertIsPresent() {
            
            dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
            ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
            Alert alert = e.apply(dr);
            alert.accept();
        }

    也可以这样用

        public void testalertIsPresentW() {
            WebDriverWait wait = new WebDriverWait(dr,10);
            dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
            ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
            Alert alert = wait.until(e);
            alert.accept();
        }

    apply用了com.google.common.base.Function的方法,而until用了FluentWait的方法。那么这两者有和区别呢,通过下面这个例子介绍,

  • 2. textToBePresentInElementLocated():文本是否出现在所定位的元素中
  •     public void testtextToBePresentInElementLocated() {
            WebDriverWait wait = new WebDriverWait(dr,10);
            dr.findElement(By.className("wait")).click();
            ExpectedCondition<Boolean> e1 = ExpectedConditions.textToBePresentInElementLocated(By.className("red"),"wait for display");
            System.out.println(e1.apply(dr));//false
    //System.out.println(wait.until(e1)); //报true }

    apply不会去等待所找元素是否存在,直接false报错no such elment。而until会在等待10秒,如果出现则返回true,否则超时。

    • 3.visibilityOfElementLocated(),该方法是判断定位的元素是否存在,使用的方法时(),apply也会去等待

      public void testvisibilityOfElementLocated() {
              WebDriverWait wait = new WebDriverWait(dr,10);
              dr.findElement(By.className("wait")).click();
              ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red"));
              
              System.out.println(wait.until(e1).getText());    //wait for display
              System.out.println(e1.apply(dr).getText());    //wait for display
              
          }

       

    • 4.visibilityOf(),判断元素是否可见

          public void testvisibilityOf() {
              WebDriverWait wait = new WebDriverWait(dr,10);
              ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOf(dr.findElement(By.className("wait")));
              
              System.out.println(e1.apply(dr).getAttribute("value"));    //wait
              //System.out.println(wait.until(e1).getAttribute("value"));//wait
          }

5.visibilityOfElementLocated(),定位的元素是否可见

    public void testvisibilityOfElementLocated() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        dr.findElement(By.className("wait")).click();
        ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red"));
        
        System.out.println(wait.until(e1).getText());    //wait for display
        System.out.println(e1.apply(dr).getText());    //wait for display
        
    }

以上列举了TestExpectedConditions的用法,鉴于apply会存在失败的情况,建议使用until。

 

PS:小插曲

下面的例子是api中关于FluentWait的用法

Sample usage:


   // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait&lt;WebDriver&gt; wait = new FluentWait&lt;WebDriver&gt;(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function&lt;WebDriver, WebElement&gt;() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

详细用法:

    @Test(enabled=false)
    public void testuntilWebElement() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        WebElement we = wait.until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("user"));
            }}) ;
        we.sendKeys("test");
    }
    
    @Test(enabled=false)
    public void testuntilBoolean() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        boolean we = wait.until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("user"));
            }}).isDisplayed();
        System.out.println(we);
    }


两者用法目的差不多,可以根据具体情况选择使用哪种方式。

 文中所用到的html例子:

 http://pan.baidu.com/s/1i3jjo3b

posted on 2015-11-16 11:59  乔叶叶  阅读(5909)  评论(0编辑  收藏  举报

导航