LoginSignup
4
1

More than 5 years have passed since last update.

RSpec + capybara で SessionStorage クリアする

Posted at

it 毎にブラウザの SessionStorage の内容をクリアしたい。
ソースコード追ったときのメモ。

JavaScript 経由でクリアするパターン

RSpec.configure do |config|
  config.prepend_after(:each, type: :feature) do
    Capybara.execute_script 'localStorage.clear()'
  end
end

参考: Capybara driver for Selenium needs to clean localStorage between tests

Capybara のメソッドで削除するパターン

Capybara.reset_sessions! を使う。

RSpec.configure do |config|
  config.prepend_after(:each, type: :feature) do
    Capybara.reset_sessions!
  end
end

reset_session!reset! のエイリアス。
https://github.com/teamcapybara/capybara/blob/master/lib/capybara/session.rb#L124-L133

reset! 自体は内部で clear_browser_state を呼び出し

  • クッキー
  • SessionStorage
  • LocalStorage

の削除をしている。
https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selenium/driver.rb#L262-L274

なお、Storage関連はオプションでクリアを有効にしないと処理がスキップされる。

  • clear_session_storage
  • clear_local_storage

4
1
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
4
1