我有一個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。 –