I am unable to click links or basically perform any action when my capybara tests run into the second it block in my rspec file.
So my code is as follows: Note - This is a basic test where I browse to a page, create a survey in the before block, then my tests in the it blocks would be to edit and view the survey.
require 'spec_helper'
include Tests # - this is a module where I include the Capybara DSL
describe "Survey Tests" do
before :all do
visit('http://mytestenv.com')
fill_in('login', :with => 'testaccount@test.com')
fill_in('password', :with => 'TestyTest')
click_button('login_button')
all('a.edit', :text => 'Edit')[0].click
click_link('Polls & Feedback')
click_link('Session Polls')
click_link('Create New Session Poll')
fill_in('question', :with => "New Question")
choose('User can type a response')
click_button('Add')
end
describe "View and Edit polls" do
it "allows you to view polls" do
click_link('Polls & Feedback')
click_link('View Poll')
should have_no_css('.form_error')
should have_content('Add another question')
should have_content('Results')
end
it "allows you to edit a poll question" do
click_link('Edit')
first('a.blueb').click
find('.checkbox').click
click_link("Add an answer")
fill_in('options[]', :with=>"Multiple answers")
click_button('Save Changes')
should have_no_css('.form_error')
end
So, what happens when I use webkit (or any of the other drivers such as chrome) is that it will run through my before statement no problem, it will then run through the first it block without any issue but when it enters the second it block, it then craps outs and says it cant find the Edit link. However if I put the click_link('Edit) at the bottom of the first it statement (after should have_content('Results')), then it will click the link no problem. So it just happens when it enters the second it statement block. Interestingly if I put this:
session = Capybara::Session.new(:selenium)
at the top of my test spec file and comment out the webkit driver info in my spec helper file the test will run and complete without any problem using firefox.
My spec helper file is as follows:
require 'yaml'
require 'capybara'
require 'capybara/rspec'
require 'rspec-expectations'
require 'config'
require 'capybara-webkit'
require 'selenium/webdriver'
########### - chrome driver - ####################### #
#Capybara.default_selector = :css#
#Capybara.default_driver = :selenium#
#Capybara.run_server = false#
#Capybara.register_driver :selenium do |app|#
# Capybara::Selenium::Driver.new(app, :browser => :chrome)
#end
############# - Poltergeist - #######################
#Capybara.default_driver = :poltergeist##Capybara.javascript_driver = :poltergeist##
#Capybara.run_server = false##
#Capybara.register_driver :poltergeist do |app|##
#Capybara::Poltergeist::Driver.new(app, {:inspector => true})
#end
############### - Capybara WebKit - ##################
Capybara.default_driver = :webkit
Capybara.javascript_driver = :webkit
Capybara.run_server = false
#This will including the Capybara DSL in the tests
module Tests
include Capybara::DSL
end
Does anyone have any idea what I am doing wrong? I have tried with various different driver such as chrome, firefox, poltergeist and capybara webkit but always the same issue.
I am using ruby 1.9.3, - capybara versions installed are 2.1.0, and 1.1.4.
Any help would be greatly appreciated!!
Thanks
The problem is that when you do require capybara/rspec
it loads a hook that resets your session/browser after each test (ie it block).
If you take this bare test suite, you can see the problem. Notice that when the second test is run, the page is no longer the same as the end of the first test. The session has been reset.
require 'capybara'
require 'capybara/rspec'
require 'rspec/autorun'
Capybara.default_driver = :selenium
include Capybara::DSL
describe 'stuff' do
it '1' do
visit('http://www.google.ca')
puts page.current_url
#=> "http://www.google.ca/"
end
it '2' do
puts page.current_url
#=> "about:blank"
end
end
As a workaround, you can monkey patch the reset_sessions!
method to do nothing.
As seen below, after applying this monkey patch, test 2 continues the browser from where test 1 left off.
require 'capybara'
require 'capybara/rspec'
require 'rspec/autorun'
Capybara.default_driver = :selenium
include Capybara::DSL
module Capybara
def self.reset_sessions!
# Do nothing
end
end
describe 'stuff' do
it '1' do
visit('http://www.google.ca')
puts page.current_url
#=> "http://www.google.ca/"
end
it '2' do
puts page.current_url
#=> "http://www.google.ca/"
end
end