我有一个RSpec特性规范来测试我的应用程序的登录。当我在RSpec中使用Capybara运行它时,它就会通过,但是当我尝试使用Capybara-webkit运行js: true
标记时,它就失败了。这是一个问题,因为我的整个应用程序背后的登录,如果我不能让这一点运行,我不知道如何为其余的应用程序的功能规格。
以下是我尝试过的:
database_cleaner.rb
文件在下面。xvfb-run -a bin/rspec spec/features/log_in_spec.rb
(似乎与正常使用Headless运行它没有什么不同)我如何让我的登录规范在Capybara-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
spec/support/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
spec/support/headless.rb
RSpec.configure do |config|
config.around type: :feature do |example|
Headless.ly do
example.run
end
end
end
spec/support/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
下面是我运行测试时调试Capybara-webkit的输出的要点。看起来好像是在反复尝试同样的事情。
UPDATE我删除了我的Capybara.app_host
设置,而非JS测试仍然可以通过,但是当我在capybara-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版本,这使得它失败了。我怎样才能让它成功?
发布于 2016-04-28 18:15:01
这太难了。但问题是,我在我的application.rb中设置了application.rb,这是愚蠢的,而不是把它放在production.rb和development.rb中,就像一个普通人一样。
我还设置了凯巴拉-webkit的app_host
,事实证明,我不需要这么做。在删除它之后,并运行了带有调试on的capybara-webkit,我看到它试图从http://localhost:45362/login
(或任何端口)重定向到https://localhost:45362/login
(请注意https
!)这导致了DOM 18的安全错误或者其他什么,这让它窒息了。我关掉了force_ssl
,现在它工作起来像个冠军。希望这能帮你别把头发扯掉。
发布于 2016-04-26 14:36:07
https://stackoverflow.com/questions/36867975
复制相似问题