【发布时间】:2012-03-06 16:58:08
【问题描述】:
我目前正在使用 Selenium 创建一些测试用例,但遇到了一个问题。
在我的测试用例中,我尝试浏览的网站有一个小表单和一个搜索按钮。填写表格并单击按钮没有问题。点击问题后问题就来了。
一旦点击按钮,就会调用这个函数:
function muestraEspera(){
document.getElementById("divCargando").style.display = "";
}
基本上,这会使包含“正在加载”图像的 DIV 可见,因此访问者只能看到图像,并且在完成之前无法实际看到加载内容的网站(内容是使用 ajax 加载的)。
一旦内容被加载,这个函数就会被执行:
function escondeEspera(){
document.getElementById("divCargando").style.display = "none";
}
这基本上隐藏了“正在加载”的 DIV,以便访问者可以看到结果。
现在,我不能使用 SLEEPS,因为加载时间或多或少,而且我需要网站的实际执行时间。有什么方法(使用 java - junit4)让 selenium 等到第二个函数执行后再继续下一步?
编辑:我确实使用 Selenium RC。要启动我使用的驱动程序:
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
selenium.start();
}
最后,Pavel Janicek 给出的最适合我的解决方案是:
boolean doTheLoop = true;
int i = 0;
while (doTheLoop){
i= i+200;
Thread.sleep(1000);
if (i>30000){
doTheLoop = false;
}
if (!selenium.isVisible("id=divCargando")){
doTheLoop = false;
}
if (selenium.isVisible("id=divCargando")){
doTheLoop = true;
}
}
【问题讨论】:
标签: java javascript function selenium wait