Rails integration test with selenium as webdriver - can't sign_in

不想你离开。 提交于 2019-11-28 06:50:05

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!