[英]Selenium: how to wait for RichFaces ajax requests to complete
我使用硒来测试JSF / RichFaces应用程序。 由于“找不到元素”错误,测试随机失败。 如Selenium中所述:间歇性的“找不到元素”问题 ,但仅与jQuery ajax调用有关。
这里的挑战是使用selenium.waitForCondition(jsExpression, timeout)
使硒测试执行等待所有ajax请求完成。 使用RichFaces Ajax调用时最好的jsExpression是什么?
我调查了为a4j:status生成的html。 下面的代码现在可以完成工作,它比wait()
语句更好,但是我正在寻找更好的解决方案。
// depends on <a4j:status> present in the page under test
selenium.waitForCondition(
"selenium.browserbot.getCurrentWindow().document.getElementById(
"_viewRoot:status.start\").style.display == 'none'",
"3000");
我曾经发现自己和你处于同样的境地。 此外,我有很多实际发现的元素,但是它们是不可见的-JSF还不够快,无法使其可见。 另外,我厌倦了一次又一次地写selenium
。
因此,我坐下来并在下面编写了代码。 它等待所有元素出现在页面上并在与它们进行交互之前可见(或在超时后失败)。 从那以后我移到了WebDriver,所以我没有原始代码,但这是这样的:
public static long WAIT = 10000; // ten seconds
private void waitForElement(String locator) {
long targetTime = System.currentTimeMillis() + WAIT;
boolean found;
do {
found = selenium.isElementPresent(locator) && selenium.isVisible(locator);
} while (!found && (targetTime < System.currentTimeMillis()));
if (!found) {
throw new SeleniumException("Element " + locator + " not found");
}
}
public void click(String locator) {
waitForElement(locator);
selenium.click(locator);
}
public void type(String locator, String text) {
waitForElement(locator);
selenium.type(locator, text);
}
至于waitForCondition()
这应该是检测元素是否存在的代码:
String locator = "id=anything";
String script =
"var retValue = true;" +
"try {" +
" selenium.browserbot.findElement('" + locator + "');" +
"} catch(e) {" +
" retValue = false;" +
"}" +
"retValue;";
selenium.waitForCondition("!!selenium.browserbot.findElement('" + locator + "')", "10000");
selenium.click(locator);
以及普通的JavaScript:
var retValue = true;
try {
selenium.browserbot.findElement('" + locator + "');
} catch(e) {
retValue = false;
}
retValue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.