Selenium - 等待JavaScript函数执行后再继续

4

我目前正在使用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;
          }      
     }
3个回答

4

您可以使用waitForCondition方法。
如果您正在使用WebDriver,您可以尝试使用WebDriverBackedSelenium或FluentWait。
我认为这个链接会有所帮助。


你会怎样使用它?我尝试使用waitForCondition,但是无法使其工作... :( - Kevin Maschke
waitForCondition似乎与Selenium-IDE相关,如果使用WebDriver方法将无法正常运行。 - Pavel Janicek

3

你应该尝试这个。它会等待指定的超时时间。要等待的内容在FluentWait对象中指定。它会一直等到Boolean变为true。所以如果你的元素不再可见,Boolean就会变为true并且方法停止等待。这个好处是它每秒只会询问一次你的元素是否可见,而不是尽可能快地询问,这没有意义。

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();
        }
    });
}

编辑:根据你在评论和编辑中提到的,似乎你正在使用Selenium 1。WebDriver 是 Selenium 2 的一部分。所以只需像这样获取已封装的驱动程序:

Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
CommandExecutor executor = new SeleneseCommandExecutor(selenium); 
WebDriver driver = new RemoteWebDriver(executor, new DesiredCapabilities());

使用 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(200, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); wait.until(new Function() { public Boolean apply(WebDriver driver) { WebElement element = driver.findElement(divCargando); return !element.isDisplayed(); } }); 我得到了这个错误:"Driver cannot be resolved to a variable"。 - Kevin Maschke
你能提供一份堆栈跟踪吗?driver是一个WebDriver实例,例如FirefoxDriver或RemoteWebDriver。 - tester
问题在于我使用的是Selenium RC:selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url"); - Kevin Maschke
或者更一般地说 - 你能提供如何初始化WebDriver的方式吗? - Pavel Janicek
你正在使用Selenium 1。只需在DefaultSelenium之后添加以下内容: CommandExecutor executor = new SeleneseCommandExecutor(selenium); WebDriver driver = new RemoteWebDriver(executor, new DesiredCapabilities()); - tester
显示剩余7条评论

2

** EDIT 3**

So we have:

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;
      }      
}

希望你不会陷入无限循环。

我从未使用过DefaultSelenium,因此请像使用click()函数一样使用isVisible()函数。


这两种方法都给我报了这些错误:"无法解析驱动程序"和"The method isVisible() is undefined for the type WebElement"。 - Kevin Maschke
我忘了提到 - driver 是 WebDriver 的实例。我稍微更新一下答案。 - Pavel Janicek
我使用以下代码来启动Selenium RC驱动程序:selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url"); - Kevin Maschke
非常感谢!这个方法很有效!我刚刚添加了以下代码:if (selenium.isVisible("id=divCargando")){ doTheLoop = true;} 这样一来,当div被隐藏后,下一步就可以顺利执行了!非常感谢! - Kevin Maschke
哎呀!终于有点进展了 :) 祝你测试愉快 ;) - Pavel Janicek
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接