【发布时间】:2016-11-14 23:48:38
【问题描述】:
我正在尝试构建一种更好的方法来等待每次点击后加载页面。 目前我使用的是这样的:
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 方法(通过 selenium 命令计算表中的行数)时,它会显示为零,而实际视觉效果不是完全为零,计数行的命令工作正常,这里是代码,如果你想检查它:
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 方法涵盖了所有内容,我希望你能给我额外的声明,我可能错过了。 提前致谢。
【问题讨论】:
-
能否提供应用程序或应用程序的任何特定框架的链接?
-
对不起,我不被允许。你能告诉我你在找什么吗?
-
而不是检查所有这些条件来确定每次单击后会发生什么......比如是否正在运行任何加载程序图标或正在显示某些文本。如果是这样,请等待该元素可见/不可见......并且我们如何检查您的功能是否正常工作。它可能适用于某些网站,但不适用于其他网站或您的应用程序..您至少可以告诉构建应用程序的框架[angular-js,ext-js ..)
-
我已经尝试过使用 element visible ,它没有帮助,因为该元素是可见的,但是在搜索时需要加载它。
-
是表 jquery-datatable 还是 jtable 或任何其他表框架
标签: javascript java jquery ajax selenium