2016-04-26 94 views
0

我有一个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版本,这使它失败。我如何让它成功?

+0

您的use_transactional_fixtures和database_cleaner配置看起来适合我进行进程外测试,所以我不认为连接共享hack应该是必需的。这就是说我不知道​​问题是什么。我会尝试save_and_open_page和save_and_open_screenshot。 –

回答

0

这是一个很难的。但问题是我在我的application.rb中设置了force_ssl = true,而不是像普通人一样将它放在production.rb和development.rb中。

我也设置了Capybara-webkit的app_host,事实证明,我并不需要这么做。删除后,运行capybara-webkit的调试,我看到它试图从http://localhost:45362/login(或任何端口)重定向到https://localhost:45362/login(请注意https!),并且这导致DOM 18安全错误或任何,并且这让它窒息。我关掉了force_ssl,现在它像一个冠军。希望这可以帮助你不把你的头发掉。

1
  1. 第一个原因是该记录后保存过程C =不守在普通视图的密码,但目前看来,正是在这种情况下,良好:

    user = create(:admin) 
    # ... 
    user = User.first # wrong way 
    #... 
    fill_in 'Password', with: 'password' 
    
  2. 第二个原因在失败登录的是工厂女孩和水豚使用单独的连接,因此一个会话中创建的数据在另一个会话中不可用。要修复它,请按照here所述使用单个连接修补程序(将其放置到规范/支持)。

+0

我试过了,没有工作。但我认为它的价值没问题,我添加了'print'发送电子邮件:#{find('#user_email')。value} \ n“'和'print”发送密码:#{find('#user_password') .value} \ n“'并在调试器输出中看到正确的值。 –

+0

@DavidHam啊,回想一下,你有使用单个连接补丁吗? –

+0

不,那是什么? –