Search code examples
rubycucumberwait

Making Ruby wait until web page has changed


I have a test in Ruby/Cucumber that runs over several consecutive pages of a webservice. I need to make it wait until one particular page has done its thing and closed in order to check the content of the page that follows.

Everything else works fine, apart from when the test is looking at the wrong page.

I've seen there are wait & until methods, but I don't know how to apply them to checking the current URL - how can I make my program wait until the page to be checked has opened?

Any help would be greatly appreciated.

Thanks for the comments. The tests are run selenium webdriver and Firefox browser. Hope this makes it clearer.

The code immediately before I want it to wait is:

def confirm_payment
    within_frame(find('iframe')) do
    find_by_id('CompanyLogo').click
    end
end

Which is the last step in this block:

if @pymnt_rqd == "y"
   confirm_customer_details
   enter_payment_card_details
   confirm_payment
#wait here until the page has turned to the response screen
  end

And I need it to wait until the page has turned before going to this:

#Check the text on the response screen against the csv file.
  check_response_screen

Solution

  • To wait for a new page, you could wait for a new body:

    require 'selenium-webdriver'
    
    driver = Selenium::WebDriver.for :chrome
    wait = Selenium::WebDriver::Wait.new(timeout: 10)
    
    driver.navigate.to "http://stackoverflow.com/"
    
    # get the body element
    body = driver.find_element(:css, "body")
    
    # do something to change the page
    ...
    
    # wait for a new page (new body)
    wait.until {driver.find_element(:css, "body") != body}