使用Selenium作为WebDriver进行Rails集成测试-无法sign_in [英] Rails integration test with selenium as webdriver - can't sign_in
问题描述
嗨
我有一个非常简单的集成测试
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
当我将Capybara驱动程序设置为rack_test
时,测试通过,但是当我将其设置为selenium
时,它失败,并显示无效的电子邮件或密码".在登录页面上(我正在使用Devise).我在做什么错了?
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?
推荐答案
当我将Capybara驱动程序设置为rack_test时,测试通过,但是当我将其设置为selenium时,它将失败,并显示无效的电子邮件或密码".在登录页面上(我正在使用Devise).我在做什么错了?
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?
您必须检查use_transactional_fixtures
.使用事务性固定装置时,因为Selenium(或任何不是Rack::Test
的外部驱动程序)无法访问已写入数据库的信息. (因为交易尚未提交")
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")
您可以使用以下方法在test_helper.rb
内部解决此问题:
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.
这篇关于使用Selenium作为WebDriver进行Rails集成测试-无法sign_in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!