deep
deep

Reputation: 23

How can I implement wait_for_page_to_load in Selenium 2?

I am new to automated web testing and I am currently migrating from an old Selenium RC implementation to Selenium 2 in Ruby. Is there a way to halt the execution of commands until the page gets loaded, similar to "wait_for_page_to_load" in Selenium RC?

Upvotes: 2

Views: 4532

Answers (3)

Jake M
Jake M

Reputation: 544

I fixed a lot of issues I was having in that department adding this line after starting my driver

driver.manage.timeouts.implicit_wait = 20

This basically makes every failed driver call you make retry for maximum 20 seconds before throwing an exception, which is usually enough time for your AJAX to finish.

Upvotes: 4

bbbco
bbbco

Reputation: 1540

Try using Javascript to inform you!

I created a couple methods that checks via our javascript libraries and waits to see if the page has finished loading the DOM and that all ajax requests are complete. Here's a sample snippet. The javascript you will need to use is just going to depend on your library.

Selenium::WebDriver::Wait.new(:timeout => 30).until { @driver.execute_script("[use javascript to return true once loaded, false if not]"}

I then wrapped these methods in a clickAndWait method that clicks the element and calls the waitForDomLoad and waitForAjaxComplete. Just for good measure, the very next command after a clickAndWait is usually a waitForVisble element command to ensure that we are on the right page.

# Click element and wait for page elements, ajax to complete, and then run whatever else
def clickElementAndWait(type, selector)
    @url = @driver.current_url
    clickElement(type, selector)
    # If the page changed to a different URL, wait for DOM to complete loading
    if @driver.current_url != @url
      waitForDomLoad
    end
    waitForAjaxComplete
    if block_given?
      yield
    end
end

Upvotes: 2

Marc-André Lafortune
Marc-André Lafortune

Reputation: 79612

If you are using capybara, whenever you are testing for page.should have_content("foo"), capybara will not fail instantly if the page doesn't have the content (yet) but will wait for a while to see if an ajax call will change that.

So basically: after you click, you want to check right away for have_content("some content that is a consequence of that click").

Upvotes: 1

Related Questions