WebRat + Selenium WebDriver: wait for ajax to finish

We use Webrat with Selenium2.0, as well as WebDriver in our application.

WebDriver handles page reloads very well and does not start the following steps if the browser reloads the entire page. The problem is that this mechanism does not work with Ajax requests. WebDriver does not make any downtime when there are some after pressing () or change ().

Can anyone suggest how to make webdriver inactive until the end of all the ajax requests on the page?

+5
source share
3 answers

We ended up writing a layer over selenium that handled this script, completing the calls in an optional loop. Therefore, when you do this:

@browser.click "#my_button_id"

it will do something similar to what AutomatedTester suggested above:

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

the shell also did other things, for example, allowing you to search by the input button / label, etc. (therefore, the shell not only existed for synchronization problems, it was just one of the things we put in there.)

+3
source

Sorry my Ruby, but you need to try to find the object, and if it's not there, just wait until it returns. What the following code should do: a wait loop every second for a minute, trying to figure out if the driver can find the element with the identifier idOfElement, and then, if it cannot, it should throw an error

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

mtd (wrapper) .

0

All Articles