Selenium-等待Javascript函数执行,然后继续
我目前正在用Selenium创建一些测试用例,我遇到了一个问题 在我的测试案例中,我尝试浏览的网站有一个小表单和一个按钮可供搜索。填写表单并单击按钮没有问题。单击问题后,问题就会出现 单击按钮后,将调用此函数:Selenium-等待Javascript函数执行,然后继续,java,javascript,function,selenium,wait,Java,Javascript,Function,Selenium,Wait,我目前正在用Selenium创建一些测试用例,我遇到了一个问题 在我的测试案例中,我尝试浏览的网站有一个小表单和一个按钮可供搜索。填写表单并单击按钮没有问题。单击问题后,问题就会出现 单击按钮后,将调用此函数: function muestraEspera(){ document.getElementById("divCargando").style.display = ""; } 基本上,这会使包含“加载”图像的DIV可见,因此访问者只看到图像,在完成之前无法看到网站加载内容(使用ajax加
function muestraEspera(){
document.getElementById("divCargando").style.display = "";
}
基本上,这会使包含“加载”图像的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;
}
}
您可以使用如果您使用的是WebDriver,您可以尝试WebDriverBackedSelenium或FluentWait。
我想会有帮助的。**编辑3** 因此,我们:
DefaultSelenium selenium = new DefaultSelenium("localhost",4444,"*iexplore", "websiteURL");
您仍然可以像这样使用命令isVisible
:
boolean doTheLoop = true;
int i = 0;
while (doTheLoop){
i = i+200;
Thread.sleep(200);
if (i>30000){
doTheLoop = false;
}
if (!selenium.isVisible("id=the ID Of element")){
doTheLoop = false;
}
}
Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
CommandExecutor executor = new SeleneseCommandExecutor(selenium);
WebDriver driver = new RemoteWebDriver(executor, new DesiredCapabilities());
希望你不会陷入无限循环
我从未使用过DefaultSelenium,因此请使用
isVisible()
函数,就像使用click()
函数一样。您应该试试这个。它等待指定的超时。它等待的内容在FluentWait对象中指定。它等待布尔值变为真。因此,如果您的元素不再可见,布尔值将变为true,方法将停止等待。这样做的好处是,它只会每隔1秒询问元素是否可见,而不是尽可能快地询问,这毫无意义
public static void wait(WebDriver driver, int timeout, final By locator){
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
wait.until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(locator);
return !element.isDisplayed();
}
});
}
使用------FluentWait wait=new FluentWait(驱动程序)。使用超时(200,TimeUnit.SECONDS)。轮询间隔(1,TimeUnit.SECONDS)。忽略(NoSuchElementException.class);wait.until(new Function(){public Boolean apply(WebDriver driver){WebElement element=driver.findElement(divCargando);return!element.isDisplayed();}}});------我确实遇到这样的错误:“驱动程序无法解析为变量”您能提供堆栈跟踪吗?驱动程序是一个WebDriver实例,比如FirefoxDiver或RemoteWebDriver;或者更一般地说,您可以提供如何初始化WebDriver吗?您可以使用Selenium 1。只需在DefaultSelenium之后添加以下内容:CommandExecutor executor=new SeleneseCommandExecutor(selenium);WebDriver driver=新的RemoteWebDriver(执行器,新的DesiredCapabilities());这两种方法都给了我这些错误:“无法解析驱动程序”和“类型WebElement的方法isVisible()未定义”。。我忘了提到-
driver
是WebDriver的实例。我会更新一下答案,我确实使用了:selenium=newdefaultselenium(“localhost”,4444,“*iexplore”,“websiteurl”);启动驱动程序。再次编辑。我将提供找到的最快解决方案:WebDriver=selenium.getWrappedDriver()代码>使用此解决方案只会遇到一个问题:“类型Selenium的getWrappedDriver()方法未定义”您将如何使用它?我用waitForCondition试过了,但没能用上…:(waitForCondition似乎与Selenium IDE相关。如果使用WebDriver方法,则将不起作用。