我目前正在使用 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;
}
}
最佳答案
您可以使用 waitForCondition
如果您使用的是 WebDriver,则可以尝试 WebDriverBackedSelenium 或 FluentWait。
我想this link会有帮助。
关于java - Selenium - 在继续之前等待 Javascript 函数执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9291898/