2010-02-03 63 views
5

我们在我们的应用程序中使用Selenium2.0或WebDriver运行Webrat。WebRat + Selenium WebDriver:等待Ajax完成

WebDriver很好地处理页面重新加载,并且如果浏览器正在重新加载整个页面,则不会开始下一步。问题在于这种机制不适用于Ajax请求。点击()或更改()后,WebDriver不会有任何空闲。

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

回答

3

我们最终在硒上编写了一个处理此场景的层,方法是将这些调用包装在可选的循环中。所以,当你会怎么做:

@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 

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

1

打扰我的Ruby,但你需要做的是试着找到对象,如果它不在那里只是等待它回来。什么下面的代码应该做的就是等待循环每秒一分钟想看看司机可以找到ID为idOfElement的元素,然后如果它不能它应该抛出一个错误

assert !60.times{ break if (driver.find_element(:id, "idOfElement) rescue false); sleep 1 } 
+0

是的,这将工作,但你必须指定你在每个Ajax请求中等待什么。我相信应该有更好的方法。 – 2010-02-03 12:58:41

0

一个独立的MTD (包装)检查元素与等待应该帮助。