Class: Capybara::Lightpanda::Cookies
- Inherits:
-
Object
- Object
- Capybara::Lightpanda::Cookies
- Defined in:
- lib/capybara/lightpanda/cookies.rb
Defined Under Namespace
Classes: Cookie
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
- #all ⇒ Object
-
#clear ⇒ Object
Lightpanda gotchas observed on current nightly: * ‘Network.clearBrowserCookies` raises `InvalidParams` (so it does NOT clear anything despite the upstream PR #1821 / >= v0.2.6 note).
- #get(name) ⇒ Object
-
#initialize(browser) ⇒ Cookies
constructor
A new instance of Cookies.
-
#load(path = "cookies.yml") ⇒ Object
Load cookies from a YAML file produced by ‘store` and re-set them.
- #remove(name:, domain: nil, path: "/") ⇒ Object
- #set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, expires: nil) ⇒ Object
-
#store(path = "cookies.yml") ⇒ Object
Persist all current cookies to a YAML file (ferrum parity).
Constructor Details
#initialize(browser) ⇒ Cookies
Returns a new instance of Cookies.
56 57 58 |
# File 'lib/capybara/lightpanda/cookies.rb', line 56 def initialize(browser) @browser = browser end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
54 55 56 |
# File 'lib/capybara/lightpanda/cookies.rb', line 54 def browser @browser end |
Instance Method Details
#all ⇒ Object
60 61 62 63 |
# File 'lib/capybara/lightpanda/cookies.rb', line 60 def all result = browser.command("Network.getCookies") (result["cookies"] || []).map { |c| Cookie.new(c) } end |
#clear ⇒ Object
Lightpanda gotchas observed on current nightly:
* `Network.clearBrowserCookies` raises `InvalidParams` (so it does NOT
clear anything despite the upstream PR #1821 / >= v0.2.6 note).
* `Network.getCookies` (no `urls` param) is scoped to the CURRENT
page's origin — cookies set on previously-visited domains are
invisible from a different page.
* `Network.getCookies` on `about:blank` raises `InvalidDomain`.
To honor Capybara’s ‘reset_session! removes ALL cookies` contract across multiple test domains (e.g. `localhost` AND `127.0.0.1`), we iterate every origin Browser has navigated to and per-origin call `Network.getCookies(urls: [origin])` then `Network.deleteCookies(url:)`. The bulk-clear call is still attempted first as a fast path / future- proofing for when upstream fixes it.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/capybara/lightpanda/cookies.rb', line 105 def clear begin browser.command("Network.clearBrowserCookies") rescue BrowserError, TimeoutError, StandardError # InvalidParams on current nightly; pre-v0.2.6 used to crash the # WebSocket. Either way, fall through to per-origin sweep. end sweep_visited_origins end |
#get(name) ⇒ Object
65 66 67 |
# File 'lib/capybara/lightpanda/cookies.rb', line 65 def get(name) all.find { || .name == name } end |
#load(path = "cookies.yml") ⇒ Object
Load cookies from a YAML file produced by ‘store` and re-set them. CDP requires either domain or url for each cookie; entries from `store` already include domain, so they round-trip cleanly. Returns true on success (intentionally not a predicate — mirrors ferrum’s API).
126 127 128 129 130 |
# File 'lib/capybara/lightpanda/cookies.rb', line 126 def load(path = "cookies.yml") # rubocop:disable Naming/PredicateMethod = YAML.load_file(path) .each { |c| (c) } true end |
#remove(name:, domain: nil, path: "/") ⇒ Object
84 85 86 87 88 89 |
# File 'lib/capybara/lightpanda/cookies.rb', line 84 def remove(name:, domain: nil, path: "/") params = { name: name, path: path } params[:domain] = domain if domain browser.command("Network.deleteCookies", **params) end |
#set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, expires: nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/capybara/lightpanda/cookies.rb', line 69 def set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, expires: nil) params = { name: name, value: value, path: path, secure: secure, httpOnly: http_only, } params[:domain] = domain if domain params[:expires] = expires.to_i if expires browser.command("Network.setCookie", **params) end |
#store(path = "cookies.yml") ⇒ Object
Persist all current cookies to a YAML file (ferrum parity). Returns the number of bytes written.
118 119 120 |
# File 'lib/capybara/lightpanda/cookies.rb', line 118 def store(path = "cookies.yml") File.write(path, all.map(&:to_h).to_yaml) end |