【发布时间】:2015-01-15 10:44:33
【问题描述】:
我有一个上传多个 zip 文件的 Selenium 脚本。这些 zip 文件的大小和上传持续时间各不相同,生成的网页可能会显示错误、成功或只是继续等待直到上传完成。
我正在尝试使用显式等待来测试前两个条件是否为真。该脚本会暂停并等待一个或其他条件为真,但直到 moveOn() 函数完成后才会触发超时异常。例如,上传 zip 可能需要 5 分钟,而我的函数会一直等待,但之后会引发超时超过 30 秒的错误。我期待超时会触发 moveOn() 函数内部的错误,所以我不确定我做错了什么。
我使用http://www.bizalgo.com/2012/01/14/timing-races-selenium-2-implicit-waits-explicit-waits/ 作为我试图做的事情的基础。
调用代码:
try{
...
String click = "jq(\"a[title='Import']\").click();";
js.executeScript(click);
moveOn();
...
} catch (Exception e) {
System.out.println("Error in importing zip " + courseName + ". Exception:" + e);
}
我的显式等待程序:
private void moveOn()
{
WebDriverWait wdw = new WebDriverWait(driver, 30);
ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>()
{
@Override
public Boolean apply(WebDriver d)
{
Boolean error = false;
Boolean success = false;
//Look for error
WebElement result1 = d.findElement(By.className("wdkErrorGoBack"));
if( "Error".equals(result1.getText()))
{
error = true;
}
//Look for success
WebElement result2 = d.findElement(By.className("sbMainPageInstructions"));
if( "The content has been imported successfully.".equals(result2.getText()))
{
success = true;
}
//Return true if either are true
return (error || success);
}
};
//Wait until either condition is met or timeout expires
wdw.until(condition);
}
任何帮助将不胜感激。谢谢。
跟进: 我已经切换到 executeAsyncScript,但我似乎仍然无法让脚本在指定的时间内超时。
driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);
String ex = "cb = arguments[ arguments.length - 1 ];\n" +
"(function(){jq(\"a[title='Import']\").click();}());\n" +
"str=jq(this).find('.wdkErrorGoBack,.sbMainPageInstructions').eq(0).text();\n"+
"cb(str);";
System.out.println(ex);
Object response = js.executeAsyncScript(ex);
【问题讨论】:
标签: selenium