26

Hi
I have a very simple integration test

require 'integration_test_helper'
Capybara.current_driver = :rack_test

class AdminSignsInTest < ActionDispatch::IntegrationTest

  test 'can sign in' do
    email = 'bob@example.com'
    password = 'secret_password'
    Admin.create email: email, password: password

    visit new_admin_session_path
    fill_in 'admin_email', with: email
    fill_in 'admin_password', with: password

    click_button I18n.t('devise.views.sign_in')

    assert_equal I18n.t('devise.sessions.signed_in'), find('p.notice').text
  end
end

When I set Capybara driver to rack_test test passes, but when I set it to selenium, it fails with 'Invalid email or password.' on the login page (I'm using Devise). What am I doing wrong?

Art Shayderov
  • 5,002
  • 1
  • 26
  • 33
  • 1
    See Avdi Grimm's article: [Configuring database_cleaner with Rails, RSpec, Capybara, and Selenium](http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/) – Jared Beck Dec 12 '13 at 20:54

3 Answers3

37

When I set Capybara driver to rack_test test passes, but when I set it to selenium, it fails with 'Invalid email or password.' on the login page (I'm using Devise). What am I doing wrong?

You'll have to check use_transactional_fixtures. When using transactional fixtures, because Selenium (or, any of the external drivers, which aren't Rack::Test) do not have access to information that has been written to the database. (as the transaction hasn't been "Committed")

You can resolve this inside of your test_helper.rb with the following:

class ActionDispatch::IntegrationTest
  self.use_transactional_fixtures = false
end

You may want, at the same time to look into something like Database Cleaner, as without transactional fixtures, your database will become untidy.

Lee Hambley
  • 6,270
  • 5
  • 49
  • 81
  • I'm sorry I forgot about this question. The problem doesn't exist any more. I spent some time trying to fix it, posted this question and the next day it just worked as it should. Something stupid like forgot to restart spork maybe. Should I try and delete this question? You are right that definitely was because of transactional_fixtures problem. I'm using shared connection hack. – Art Shayderov Jun 12 '11 at 18:24
  • 4
    Unless you've any objection, I'd appreciate the *Accept*, my answer will still help people suffering a similar problem. – Lee Hambley Jun 12 '11 at 19:30
9

From http://opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/:

You will also need DatabaseCleaner, since transactional fixtures do not work with Selenium. Add the database_cleaner gem to the :test group of your Gemfile and put the following code into spec/support/database_cleaner.rb:

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.before :each do
    DatabaseCleaner.start
  end
  config.after :each do
    DatabaseCleaner.clean
  end
end
mattwindwer
  • 929
  • 9
  • 18
  • 1
    THANK YOU OMG! setting `config.use_transactional_fixtures = false` fixed it so that I could run selenium as well as the normal Rack::Tests. Happily running Rspec/Capybara:Selenium with Device/Warden login now (and not having to use the Devise sign in form but using Warden login_as() helper instead for faster tests. THANK YOU! – FireDragon Jan 31 '13 at 05:43
0

I found mattwinder's answer worked, but I also had to comment out the line

config.use_transactional_fixtures = true

in spec/spec_helper.rb as well. Just overriding it in spec/support/database_cleaner.rb wasn't sufficient to make logins work with Selenium.

Joel
  • 2,285
  • 2
  • 21
  • 22