我想建立一個更好的方法來等待頁面加載後,每次點擊。 在我用什麼樣的瞬間是這樣的:Selenium Webdriver - 等待頁面完全加載Java和JavaScript(ajax/jQuery /動畫等)
public boolean waitForJSandJQueryToLoad() {
WebDriverWait wait = new WebDriverWait(webDriver, EnvironmentUtils.getPageLoadTimeout().intValue());
// wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
if (!((Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);"))) {
log.info("JQUERY.ACTIVE IS WORKING AT THE MOMENT! jQuery.active= " + callJS("return jQuery.active"));
}
return (Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);");
} catch (Exception e) {
// no jQuery present
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
if (!callJS("return document.readyState").toString().equals("complete")) {
log.info("document.readyState is not complete at the moment! status is->" + callJS("return document.readyState").toString());
}
return callJS("return document.readyState").toString().equals("complete");
}
};
ExpectedCondition<Boolean> animationLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
if (!callJS("return $(\":animated\").length").toString().equals("0")) {
log.info("Animation is currently executing on-page. value ->" + callJS("return $(\":animated\").length").toString());
}
return callJS("return $(\":animated\").length").toString().equals("0");
}
};
return wait.until(jQueryLoad) && wait.until(jsLoad) && wait.until(animationLoad);
}
在某些情況下,我的測試中點擊一些按鈕,並等待表之後加載後仍無法
,但是當我跑的countRowsInTable方法(該方法在計算行硒指令表),它帶來了零,而實際的視覺不爲零可言,count行的命令是否正常工作,這裏的代碼,如果你想檢查一下:
public int countDisplayedRowsInTable(String tableId) {
waitForJSandJQueryToLoad();
int count = 0;
List<WebElement> rows = webDriver.findElements(By.xpath("//table[@id='" + tableId + "']/tbody/tr"));
for (WebElement row : rows) {
if (row.isDisplayed())
count++;
}
return count;
}
我敢肯定,我用我的waitForJSandJQueryToLoad方法覆蓋了任何東西,我希望你可以給我額外的陳述,也許我錯過了。 在此先感謝。
您能否提供指向應用程序或應用程序的任何特定框架的鏈接? – Madhan
我很抱歉,但我不被允許。你能讓我知道你在找什麼嗎? –
而不是檢查所有這些條件,確定每次點擊後會發生什麼......就像加載器圖標正在運行或正在顯示某些文本一樣。如果等待該元素可見/不可見,那麼我們如何檢查您的函數正在工作或不是。它可能適用於某些網站,但不適用於其他人或您的應用程序..您可以至少告訴構建應用程序的框架[angular-js,ext-js ..] – Madhan