8
votes

I'm trying to write simple test. My problem is, that i want to wait until the page is loaded completly. At the moment i'm waiting until some elements are presen, but that is not really what i want. Is it possible to make something like this:

driver = Selenium::WebDriver.for :chrome
driver.navigate.to url
driver.wait_for_page_to_load "30000"

With Java isn't problem, but how to make it with ruby?

5

5 Answers

15
votes

This is how the Selenium docs () suggest:

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://google.com"

element = driver.find_element :name => "q"
element.send_keys "Cheese!"
element.submit

puts "Page title is #{driver.title}"

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.title.downcase.start_with? "cheese!" }

puts "Page title is #{driver.title}"
driver.quit

If that is not an option you can try the suggestion from this SO post though it would require some Javascript on top of the Ruby/Rails.

It seems that wait.until is being/has been phased out. The new suggested process it to look for the page to have an element you know will be there:

expect(page).to have_selector '#main_div_id'
5
votes

As far as I understand webdriver, you dont need to wait for page loads because WebDriver has a blocking API but you can sure set a page load timeout.

driver.manage.timeouts.page_load = 10 # seconds 
3
votes

So in Ruby, whenever you use get to open a URL, the ruby script proceeds ONLY when the page completely loads.

So in your case you would simply do :-

driver.get url
2
votes

That's not needed with WebDriver anymore.

WebElement click() and Actions click() both "wait for page load" if needed automatically.

You can use imclicit and explicit (in this order) wait instead (described at seleniumhq) if you need to wait for some ajax content for instance.

1
votes

There have been instances where either AJAX or CSS changes caused my tests to fail at times. I added these methods to my static driver instance so that I can have the test wait for certain conditions if needed. (c#)

TimedWait in the WaitForCssChange Method is basically just a Threading.Thread.Sleep This is not the most beautiful way I guess, but it works well for my needs.

For Ajax wait:

 public static void WaitForAjax()
    {
        var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(25));
        wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
    }

For CSS Changes:

public static void  WaitForCssChange(IWebElement element, string value)
    {
        int counter = 0;
        while (true)
        {
           if(element.GetAttribute("style").Contains(value) || counter > 50)
           {
               break;
           }
           TimedWait(20);
           counter++;
        }
    }