5

我们在我们的应用程序中使用 Selenium2.0 aka WebDriver 运行 Webrat。

WebDriver 可以很好地处理页面重新加载,如果浏览器正在重新加载整个页面,则不会开始下一步。问题是这种机制不适用于 Ajax 请求。当 click() 或 change() 之后有一些空闲时,WebDriver 不会做任何空闲。

谁能建议如何让 webdriver 空闲直到页面上所有 ajax 请求结束?

4

3 回答 3

3

我们最终在 selenium 上编写了一个层,通过将调用包装在一个可选循环中来处理这种情况。所以当你这样做时:

@browser.click "#my_button_id"

它会做类似于上面AutomatedTester建议的事情:

class Browser
  def click(locator)
    wait_for_element(locator, :timeout => PAGE_EVENT_TIMEOUT)
    @selenium.click(locator)
  end

  def wait_for_element(locator, options)
    timeout = options[:timeout] || PAGE_LOAD_TIMEOUT
    selenium_locator = locator.clone
    expression = <<EOF 
      var element;
      try {
        element = selenium.browserbot.findElement('#{selenium_locator}');
      } catch(e) {
        element = null;
      };
      element != null;
EOF
    begin
      selenium.wait_for_condition(expression, timeout)
    rescue ::Selenium::SeleniumException
      raise "Couldn't find element with locator '#{locator}' on the page: #{$!}.\nThe locator passed to selenium was '#{selenium_locator}'"
    end
  end
end

包装器还做了其他事情,比如允许通过按钮/输入标签等进行搜索(所以包装器不仅存在时间问题,这只是我们放在那里的东西之一。)

于 2010-09-09T15:36:58.347 回答
1

对不起,我的 Ruby,但您需要做的是尝试找到该对象,如果它不存在,则等待它回来。下面的代码应该做的是每秒等待循环一分钟,试图查看驱动程序是否可以找到具有 ID 的元素,idOfElement如果找不到,则应该抛出错误

assert !60.times{ break if (driver.find_element(:id, "idOfElement) rescue false); sleep 1 }
于 2010-02-03T11:31:29.280 回答
0

一个单独的 mtd(包装器)来检查等待的元素应该会有所帮助。

于 2010-08-17T09:09:42.187 回答