我目前正在使用Selenium创建一些测试用例,但遇到了一个问题。
在我的测试用例中,我试图通过一个小表单和一个搜索按钮来遍历网站。没有问题填写表单并点击按钮。问题出现在点击按钮后。
点击按钮后,将调用此函数:
function muestraEspera(){
document.getElementById("divCargando").style.display = "";
}
基本上,这会显示一个包含"loading"图像的DIV,以便访问者只能看到图像,并且在内容加载完之前无法实际看到网站加载内容(使用ajax加载内容)。
一旦内容加载完成,就会执行此函数:
function escondeEspera(){
document.getElementById("divCargando").style.display = "none";
}
这基本上是隐藏“loading”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;
}
}