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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM