I figured it out.
I needed to add the following metho to tell Selenium to wait until all of the ajax calls were completed.
I put this method in my spec/spec_helper.rb file and made sure to have require 'spec_helper' in the file.
Here is the method in spec_helper.rb:
#needed for selenium ajax testing
def wait_for_ajax(timeout=5000)
js_condition = 'selenium.browserbot.getUserWindow().jQuery.active == 0'
@selenium_driver.wait_for_condition(js_condition, timeout)
end
#needed for selenium ajax testing
def selenium_setup
@verification_errors = []
@selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://localhost:3000/",
:timeout_in_second => 60
#return @selenium_driver
end
As you can see above I also moved the setup code for the selenium_driver to the spec_helper.rb file to clean up my code and make it more DRY:
Here are my integration tests file:
require 'spec_helper'
describe "Board form" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
selenium_setup
@selenium_driver.start_new_browser_session
end
after(:all) do
@selenium_driver.close_current_browser_session
@verification_errors.should == []
end
describe "create board" do
describe "failure" do
it "test_ home page form" do
page.open "/"
("Happy Birthday Greetings | Home").should == page.get_title
page.is_element_present("board_bp_name").should be_true
page.is_text_present("Name").should be_true
("Happy Birthday Greetings | Home").should == page.get_title
page.click "board_submit"
wait_for_ajax
page.is_element_present("board_bp_name").should be_true
page.is_text_present("Name").should be_true
page.is_element_present("board_birthday_1i").should be_true
page.is_element_present("board_submit").should be_true
page.is_text_present("exact:Oops, looks like 1 error occured: \n Hey whose birthday is it? Please enter a name.").should be_true
page.is_element_present("error_explanation").should be_true
end
end
describe "success" do
it "should create a new board for a properly filled in form and show the correct flash message" do
page.open "/"
page.type "board_bp_name", "Test User"
page.select "board_birthday_2i", "label=October"
page.select "board_birthday_1i", "label=1967"
page.select "board_birthday_3i", "label=7"
page.click "board_submit"
wait_for_ajax
page.wait_for_page_to_load("30000")
page.get_location().should =~ /boards\/\d/i
page.is_element_present("css=div.flash.success").should be_true
page.is_text_present("Your friend's board has been created.").should be_true
page.is_text_present("Test User").should be_true
page.is_text_present("43").should be_true
page.is_element_present("greeting_link").should be_true
page.is_text_present("Add greeting").should be_true
end
end
end
end