Skip to content

Instantly share code, notes, and snippets.

@joakimk
Last active November 7, 2022 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joakimk/d6ef0a0ab9a4d5b2aee60bc48beac559 to your computer and use it in GitHub Desktop.
Save joakimk/d6ef0a0ab9a4d5b2aee60bc48beac559 to your computer and use it in GitHub Desktop.
Save and restore cookies and session with capybara and apparition
# I have not deeply researched the best way of doing this. This is just one way.
# Made this gist because there was no google result for this exact question so I had to dive into the code instead.
# Saving
cookies = browser.driver.cookies.instance_variable_get(:@browser).get_raw_cookies.map { |c| c.instance_variable_get(:@attributes) }
cookies_json = cookies.to_json
# Restoring
cookies = JSON.parse(cookies_json)
cookies.each do |c|
browser.driver.set_cookie(c.fetch("name"), c.fetch("value"), c.except("name", "value").symbolize_keys)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment