Cucumber: Clear localStorage after each scenario

Posted Almost 7 years ago. Visible to the public. Deprecated.

Capybara 3.12.0+ automatically clears localStorage and sessionStorage after each test.

Capybara clears cookies before each scenario, but not other client-side data stores. If your app is using localStorage or sessionStorage, contents will bleed into the next scenario.

Use this hook to remove all site data after each scenario:

After do
  if Capybara.current_driver == :selenium && !Capybara.current_url.starts_with?('data:')
    page.execute_script <<-JAVASCRIPT
      localStorage.clear();
      sessionStorage.clear();
    JAVASCRIPT
  end
end
Henning Koch
Last edit
Almost 7 years ago
Arne Hartherz
Keywords
site, data
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2017-07-19 08:58)