2016-07-20 14 views
1

我想在硒中執行一個操作,它將等待狀態更改爲某個「完成」狀態。硒有條件的等待與中間行爲

從概念上講,它可以在僞代碼中排列爲這樣:

public boolean waitForActionToComplete(long maxTimeoutInSeconds, int repeatTimeInSeconds, Callable<T> action, Callable<T> condition) { 
    long startTime = 0; 
    while (startTime < maxTimeoutInSeconds) 
     perform <action>;     // e.g., click on a "Refresh" button to refresh the results 
     boolean done = verify <condition>; // e.g., check whether the job status is "Done" 
     if (done)   
      return true;     // if done, then exit with TRUE 
     else 
      Thread.sleep(repeatTimeInSeconds); 
    end while; 

    return false;       // status still not complete, timeout gracefully 
} 

這種方法可以容易與ExpectedCondition和WebdriverWait/FluentWait稍微簡單的方法來實現。但是,由於框架中的某些約束,我無法精確地實現和使用像這樣的方法。以上方法都必須執行,因爲這(在框架具有這種方法簽名實現一個接口):

public void execute(final WebDriver webDriver, String... parameters) { 
// implementation here 
} 

誰能告訴我如何改造方法在上述規定的形式?

+0

您應該調查[WebDriverWait](HTTPS ://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html)和[FluentWait](https://seleniumhq.github.io/selenium/docs/ API/JAVA /組織/ openqa /硒/支撐/ UI/FluentWait.html)。 – JeffC

+0

謝謝JeffC。我很感激。這說得通。我想我沒有完成上面的問題陳述。我現在已經完成了。 – naspras

+0

您仍可以使用FluentWait進行描述。你只需要自己實現它就可以使用任何'verify '。 – JeffC

回答

1

我想在Selenium中執行一個動作,等待狀態變爲某種「完成」狀態。

這可以簡單地使用WebDriverWaitExpectedConditions.textToBePresentInElementLocated實現與其使用如下創建自己的自定義方法waitForActionToComplete: -

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.textToBePresentInElementLocated(byObject, "Finished")); 

希望它能幫助... :)