2013-10-16 65 views
0

我進入開發和新QA /自動化測試。 我想了解下面的代碼;瞭解流利硒等待

public WebElement getVisibleElement(final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod) { 
    return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until(new Function<WebDriver, WebElement>() { 
     public WebElement apply(WebDriver driver) { 
      try { 
      } catch { 
      } 
      return null; 
     } 
    }); 
} 

在我的同班同學中,我也有;

public Wait<WebDriver> fluentWait(int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod) { 

    return new FluentWait<WebDriver>(this.webDriver) 
         .withTimeout(timeoutValue, timeoutPeriod) 
         .pollingEvery(pollingInterval, pollingPeriod) 
         .ignoring(NoSuchElementException.class); 
} 

特別是我想了解的2件事;

  1. 什麼是返回fluentWait()正在做什麼?
  2. until()是什麼意思?

回答

2

他們綁在了一起,所以我不會單獨回答這樣:

硒的FluentWait方法通常只是爲了等待某個條件是真的。您可以給它許多不同的可能條件,它會繼續評估它,直到它爲真,或者超時值通過,以先到者爲準。

a)return,在大多數編程語言中只是從方法返回東西

b)until是您在FluentWait上調用的方法,以便對其進行物理評估。在此之前的一切,只是設置它,使用.until(....)告訴FluentWait實例去評估我給你的代碼。在你的情況下,我通過調用.until(....)的方法名稱(實際方法不完整),告訴FluentWait繼續嘗試從頁面獲取元素,直到它對用戶可見。

fluentWait方法僅僅建立一個FluentWait實例來使用,而apply/until代碼段設置條件你想評價一下。在您的getVisibleElement方法中,您返回WebElement,因此在您的apply代碼部分,您需要返回WebElement,一旦您滿意,用戶可以看到它。

我會假設你想要在apply內做的事情是找到使用正常的.findElement的元素,並檢查它的visible屬性。如果確實如此,則返回發現的WebElement,如果不是,則返回null以強制FluentWait繼續前進。

因此,return fluentWait僅僅是說「返回這個FluentWait返回」。其中,由於該方法已經被聲明爲返回一個實例,因此你在說「返回這個返回的任何WebElement」。