Last active
November 7, 2022 02:34
-
-
Save joakimk/d6ef0a0ab9a4d5b2aee60bc48beac559 to your computer and use it in GitHub Desktop.
Save and restore cookies and session with capybara and apparition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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