我有一个RSpec功能规格,测试我的应用程序的登录名。当我在RSpec中使用Capybara运行它时,它会通过,但是当我尝试运行使用Capybara-webkit标记为js: true
时,它会失败。这是一个问题,因为我的整个应用程序都在登录后面,如果我无法运行这个位,我不知道如何为应用程序的其余部分执行功能规格。无法使用Capybara-webkit登录,但RSpec/Capybara工作正常
这是我曾尝试:
- 安装所有水豚,WebKit的依赖listed here。我在一个建立在ruby:2.3 image上的Docker容器中运行我的应用程序,它基于Jessie。
- 设置DatabaseCleaner per this blog post。下面是我的
database_cleaner.rb
文件。 - 使用无头宝石(以下headless.rb)
- 运行RSpec的,像这样:
xvfb-run -a bin/rspec spec/features/log_in_spec.rb
(似乎并不比通常与无头运行它不同)
如何让我的登录功能水豚下工作-webkit?我的一些规格需要标记为JS,有些则不需要,但他们都需要用户登录。谢谢。
log_in_spec.rb
require 'rails_helper'
RSpec.feature "Log in", type: :feature do
scenario "as admin" do
user = create(:admin)
# Tried this instead of with Capybara, works with Capybara but not capybara-webkit
# login_as user, scope: :user, run_callbacks: false
visit root_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
find('.btn-primary').click
expect(page).to have_content('Admin')
end
end
spec_helper.rb
require 'capybara/rspec'
require 'paperclip/matchers'
RSpec.configure do |config|
Capybara.javascript_driver = :webkit
Capybara.app_host = 'https://192.168.99.101'
config.include Paperclip::Shoulda::Matchers
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
mocks.verify_doubled_constant_names = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
end
end
rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'
require 'devise'
require 'support/controller_macros'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.filter_rails_from_backtrace!
config.include Warden::Test::Helpers
config.before :suite do
Warden.test_mode!
end
config.after :each do
Warden.test_reset!
end
end
# Added headless gem and this code thanks to this post: http://stackoverflow.com/a/28706535/3043668
if ENV['HEADLESS']
require 'headless'
headless = Headless.new
headless.start
at_exit { headless.stop }
end
规格/支持/ database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
规格/支持/ headless.rb
RSpec.configure do |config|
config.around type: :feature do |example|
Headless.ly do
example.run
end
end
end
规格/支持/ capybara.rb
Capybara::Webkit.configure do |config|
config.debug = true
config.allow_unknown_urls
config.timeout = 5
config.ignore_ssl_errors
config.skip_image_loading
end
这里当我运行测试时,它的要点是debug output from Capybara-webkit。它看起来像是一遍又一遍地尝试同样的事情。
UPDATE 我打消了我的Capybara.app_host
设置和非JS测试依然通过,但是当我在水豚,WebKit的运行它,我看到这个在debig输出:
Received 0 from "https://127.0.0.1:37193/login"
Page finished with false
Load finished
Page load from command finished
Wrote response false "{"class":"InvalidResponseError","message":"Unable to load URL: http://127.0.0.1:37193/login because of error loading https://127.0.0.1:37193/login: Unknown error"}"
Received "Reset()"
Started "Reset()"
undefined|1|SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
它试图到visit("/login")
,它正在被重定向到https版本,这使它失败。我如何让它成功?
您的use_transactional_fixtures和database_cleaner配置看起来适合我进行进程外测试,所以我不认为连接共享hack应该是必需的。这就是说我不知道问题是什么。我会尝试save_and_open_page和save_and_open_screenshot。 –