4

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?

elbarto132
  • 203
  • 2
  • 7
  • 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 Sep 27 '15 at 16:27
  • i can answer that too. the log/test.log for this specified test. http://pastie.org/10447426 - looks normal – Tim Kretschmer Sep 28 '15 at 02:57
  • Capybara debug-output is displaying nothing usesful or giving us any kind of information why he should loose the session – Tim Kretschmer Sep 28 '15 at 03:07

1 Answers1

2

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
Community
  • 1
  • 1
Jarfis
  • 125
  • 11