LoginSignup
43
34

More than 5 years have passed since last update.

Capybara::Poltergeist::TimeoutErrorが稀に起こる問題の対応

Posted at

Capybara::Poltergeist::TimeoutErrorで苦しんでいて解決した方法のシェア

対象となる症状

条件1: 以下の様なエラーが出る

  1) #show User can view signle preference articles
     Failure/Error: sign_in_as candidate
     Capybara::Poltergeist::TimeoutError:
       Timed out waiting for response to {"name":"visit","args":["http://127.0.0.1:38925/user/auth/facebook/callback"]}. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the Poltergeist :timeout option to a higher value will help (see the docs for details). If increasing the timeout does not help, this is probably a bug in Poltergeist - please report it to the issue tracker.
     # ./spec/support/user_macros.rb:4:in `sign_in_as'
     # ./spec/concerns/single_preference_concern_spec.rb:28:in `block (2 levels) in <top (required)>'

条件2: timeoutの時間を60秒といったありえないぐらい長くしてもまだタイムアウトが起こる

spec/spec_helper.rb
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app, js_errors: true, timeout: 60)
    end

条件3: Time outが起こる時、そのテストはcapybaraが使われる最初の方テストである

対応方法

https://github.com/teampoltergeist/poltergeist/issues/294
によると、Timeoutの計算の際に、Asset Compileの時間が乗っかてしまっている可能性があるらしい。

なので、テストが始まる前に一度読み込んでおくと解決する。
例えば、application.jsapplication.cssがある場合は以下のようにする。

spec/spec_helper.rb
  # HACK to force asset compilation in a Rack request so it's ready for
  # the Poltergeist request that otherwise times out.
  config.before(:all) do
    if self.respond_to? :visit
      visit '/assets/application.css'
      visit '/assets/application.js'
    end
  end

速度を気にしているサイトだとjsはheader.jsfooter.jsに分かれていたりするだろうし、適宜コンパイルに時間がかかっていそうなアセットを事前に読み込んでおくと良い。

43
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
34