首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法登录Capybara-webkit,但RSpec/Capybara工作正常。

无法登录Capybara-webkit,但RSpec/Capybara工作正常。
EN

Stack Overflow用户
提问于 2016-04-26 14:31:09
回答 2查看 811关注 0票数 0

我有一个RSpec特性规范来测试我的应用程序的登录。当我在RSpec中使用Capybara运行它时,它就会通过,但是当我尝试使用Capybara-webkit运行js: true标记时,它就失败了。这是一个问题,因为我的整个应用程序背后的登录,如果我不能让这一点运行,我不知道如何为其余的应用程序的功能规格。

以下是我尝试过的:

  • 安装所有Capybara依赖项列在这里。我正在一个基于ruby:2.3图片的Docker容器中运行我的应用程序,它建立在Jessie上。
  • 设置DatabaseCleaner 根据这篇博客文章。我的database_cleaner.rb文件在下面。
  • 使用无头宝石(以下为headless.rb)
  • 像这样运行RSpec:xvfb-run -a bin/rspec spec/features/log_in_spec.rb (似乎与正常使用Headless运行它没有什么不同)

我如何让我的登录规范在Capybara-webkit下工作?我的一些规范需要标记为JS,有些则不需要,但它们都需要用户登录。谢谢。

log_in_spec.rb

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
RSpec.configure do |config|
  config.around type: :feature do |example|
    Headless.ly do
      example.run
    end
  end
end

spec/support/capybara.rb

代码语言:javascript
运行
复制
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输出中看到这一点:

代码语言:javascript
运行
复制
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版本,这使得它失败了。我怎样才能让它成功?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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,现在它工作起来像个冠军。希望这能帮你别把头发扯掉。

票数 0
EN

Stack Overflow用户

发布于 2016-04-26 14:36:07

  1. 第一个原因是保存过程c=keep后的记录在普通视图中不是密码,但在这种情况下似乎是好的: user = create(:admin) #.用户= User.first #错误的方式#。fill_in“密码”,带有:“密码”
  2. 无法登录的第二个原因是工厂女孩和水豚使用单独的连接,因此在一个会话中创建的数据在另一个会话中不可用。要修复它,请使用单个连接修补程序(将其放到规范/支持中),如所述的这里
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36867975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档