【问题标题】:Selenium - Wait for Javascript function to execute before continuingSelenium - 在继续之前等待 Javascript 函数执行
【发布时间】: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


    【解决方案1】:

    您可以使用waitForCondition
    如果你使用 WebDriver,你可以试试 WebDriverBackedSelenium 或 FluentWait。
    我认为this link 会有所帮助。

    【讨论】:

    • 你会如何使用它?我用 waitForCondition 试过了,但我无法让它工作...... :(
    • waitForCondition 似乎与 Selenium-IDE 相关。如果您使用 WebDriver 方法将无法正常工作。
    【解决方案2】:

    你应该试试这个。它一直等到指定的超时。它等待的内容在 FluentWait 对象中指定。它一直等到布尔值变为真。因此,如果您的元素不再可见,则布尔值变为真并且该方法停止等待。这样做的好处是,它仅每 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();
            }
        });
    }
    

    编辑:正如您在 cmets 和编辑中所写,您似乎使用 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 wait = new FluentWait(driver) .withTimeout(200, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS) .ignoring(NoSuchElementException 。班级); wait.until(new Function() { public Boolean apply(WebDriver driver) { WebElement element = driver.findElement(divCargando); return !element.isDisplayed(); } }); ----- 我确实收到此错误:“驱动程序无法解析为变量”
    • 你能提供一个堆栈跟踪吗? driver 是一个 WebDriver 实例,如 FirefoxDiver 或 RemoteWebDriver。
    • 问题是我使用 selenium RC: selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
    • 或更笼统地说 - 你能提供你如何初始化 WebDriver 吗?
    • 您使用 Selenium 1. 只需在 DefaultSelenium 之后添加以下内容: CommandExecutor executor = new SeleneseCommandExecutor(selenium); WebDriver driver = new RemoteWebDriver(executor, new DesiredCapabilities());
    【解决方案3】:

    ** 编辑 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;
          }      
    }
    

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

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

    【讨论】:

    • 两种方法都给我这些错误:“驱动程序无法解析”和“方法 isVisible() 未定义 WebElement 类型”..
    • 我忘了提 - driver 是 WebDriver 的实例。我会稍微更新一下答案。
    • 我确实使用:selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");启动驱动程序。硒 RC。
    • 再次编辑。我将提供最快的解决方案:WebDriver driver = selenium.getWrappedDriver();
    • 使用此解决方案的一个问题:“方法 getWrappedDriver() 未定义 Selenium 类型”
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多