4
votes

i'm using rspec/capybara in my rails project for tests, which is working fine with the default driver. But when i switch to webkit or selenium i get logged out after every request that i make. This code is working as expected, i see the logged in page 2 times:

require 'rails_helper'

feature 'test' do
  scenario 'this' do
    user = FactoryGirl.create :user
    login_as(user)
    visit root_path
    save_and_open_page
    visit root_path
    save_and_open_page
  end
end

When i set webkit or selenium as driver only the first page is the logged in version, on the second page i'm logged out:

require 'rails_helper'

feature 'test' do
  scenario 'this', driver: :webkit do
    user = FactoryGirl.create :user
    login_as(user)
    visit root_path
    save_and_open_page
    visit root_path
    save_and_open_page
  end
end

How can i fix this?

1
Have you tried tailing the log/test.log file to see what requests are coming in during the test? If so, could you share?Tom Fast
i can answer that too. the log/test.log for this specified test. pastie.org/10447426 - looks normalTim Kretschmer
Capybara debug-output is displaying nothing usesful or giving us any kind of information why he should loose the sessionTim Kretschmer

1 Answers

2
votes

I was having this exact same problem and eventually came across this question with pretty much the same problem: Why is Capybara discarding my session after one event?

The solution is to include the snippet found here in your rails_helper

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection