WebRat + Selenium WebDriver:等待ajax完成

Bog*_*iev 5 ajax selenium webdriver webrat

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

WebDriver非常好地处理页面重新加载,如果浏览器正在重新加载整个页面,则不会启动后续步骤.问题是这种机制不适用于Ajax请求.当有一些click()或change()后,WebDriver不会执行任何空闲操作.

任何人都可以建议如何使webdriver闲置,直到页面上的所有ajax请求结束?

zcr*_*r70 3

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

@browser.click "#my_button_id"
Run Code Online (Sandbox Code Playgroud)

它会做类似于上面 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
Run Code Online (Sandbox Code Playgroud)

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