遭遇した問題
PhantomJS 1.9系からPhantomJS 2.1.1にアップデートした後、既存のテストコードを実行すると次のようなエラーが出た。
Capybara::Poltergeist::ObsoleteNode:
The element you are trying to interact with is either not part of the DOM, or is not currently visible on the page (perhaps display: none is set). It's possible the element has been replaced by another element and you meant to interact with the new element. If so you need to do a new 'find' in order to get a reference to the new element.
# ./spec/features/posts_spec.rb:15:in `block (2 levels) in <top (required)>'
# ./spec/rails_helper.rb:67:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Capybara::Poltergeist::BrowserError:
# There was an error inside the PhantomJS portion of Poltergeist. If this is the error returned, and not the cause of a more detailed error response, this is probably a bug, so please report it.
#
# Poltergeist.ObsoleteNode:
# ./spec/features/posts_spec.rb:15:in `block (2 levels) in <top (required)>'
エラーが発生する箇所
問題が発生するのはattach_file
メソッドを使っている箇所。
file_path = "#{Rails.root}/spec/fixtures/images/sample.png"
attach_file '添付画像', file_path
なおかつ、View側ではjquery-fileuploadプラグインを使ってファイルをAjaxでアップロードしていた。
原因と解決策
CapybaraとPoltergeistのバージョンが古かった。最新版をインストールしたら直りました。。
Before/After
- Capybara 2.5.0 => 2.8.1
- Poltergeist 1.8.1 => 1.10.0
教訓
PhantomJSのバージョンを上げたら、CapybaraとPhantomJSも最新版にアップデートしましょう!!