English 中文(简体)
WebRat+Selenium WebDriver: wait for ajax to be completed
原标题:

We are running Webrat with Selenium2.0 aka WebDriver in our application.

WebDriver handles page reloading very well and do not start next steps if the browser is reloading entire page. The problem is that this mechanism doesn t work with Ajax requests. WebDriver doesn t do any idle when there some after click() or change().

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

最佳回答

We ended up writing a layer over selenium that handled this scenario by wrapping the calls in an optional loop. So when you d do:

@browser.click "#my_button_id"

it would 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: #{$!}.
The locator passed to selenium was  #{selenium_locator} "
    end
  end
end

the wrapper also did other things, like allowing to search by the button/input label etc. (so the wrapper didn t only exist for the timing issues, this was just one of the things we put in there.)

问题回答

Excuse my Ruby but what you need to do is try find the object and if its not there just wait for it to come back. What the code below should do is wait loop every second for a minute trying to see if the driver can find the element with the ID idOfElement and then if it can t it should throw an error

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

A separate mtd (wrapper) to check for the element with a wait should help.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签