How can I implement wait_for_page_to_load in Selenium 2?

I am new to automated web testing and am currently transitioning from an old implementation of Selenium RC in Selenium 2 to Ruby. Is there a way to stop the execution of commands before the page loads, similar to " wait_for_page_to_load " in Selenium RC?

+2
ruby selenium webdriver
source share
3 answers

Try using Javascript to report it!

I created a couple of methods that check through our javascript libraries and wait for the page to finish loading the DOM and that all ajax requests are complete. Here is an example fragment. The javascript you need to use just depends on your library.

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

Then I wrapped these methods in the clickAndWait method, which clicks on the element and calls waitForDomLoad and waitForAjaxComplete. For a good estimate, the very next command after clickAndWait is usually the waitForVisble element waitForVisble to make sure that we are on the correct 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 
+2
source share

I fixed a lot of problems that I had in this section by adding this line after starting my driver

 driver.manage.timeouts.implicit_wait = 20 

This basically makes every failed driver attempt to retry for no more than 20 seconds before throwing an exception, which is usually enough to complete AJAX.

+4
source share

If you use capybara , whenever you test page.should have_content("foo") , capybara will not work instantly if there is no content on the page (yet), but wait a while to see if the ajax call will change this.

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

+1
source share

All Articles