2016-04-26 84 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

不,那是什麼? –