Skip to content

Commit 99cfa84

Browse files
committed
feat: Implement store/load cookies to yaml
1 parent 969a935 commit 99cfa84

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- `Ferrum::Browser#close` closes browser gracefully issuing a CDP command, doesn't clean up ruby resources.
77
- `Ferrum::Node#remove` removes node from DOM tree.
88
- `Ferrum::Node#exists?` check whether the node in ruby world still exists in the DOM tree.
9+
- `Ferrum::Cookies#store` stores all cookies of current page in a file.
10+
- `Ferrum::Cookies#load` Loads all cookies from the file and sets them for current page.
911

1012
### Changed
1113

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,25 @@ Removes all cookies for current page
882882
page.cookies.clear # => true
883883
```
884884

885+
#### store(path) : `Boolean`
886+
887+
Stores all cookies of current page in a file.
888+
889+
```ruby
890+
# Cookies are saved into cookies.yml
891+
page.cookies.store # => 15657
892+
```
893+
894+
#### load(path) : `Boolean`
895+
896+
Loads all cookies from the file and sets them for current page.
897+
898+
```ruby
899+
# Cookies are loaded from cookies.yml
900+
page.cookies.load # => true
901+
```
902+
903+
885904
## Headers
886905

887906
`page.headers`

lib/ferrum/cookies.rb

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "yaml"
34
require "ferrum/cookies/cookie"
45

56
module Ferrum
@@ -169,6 +170,32 @@ def clear
169170
true
170171
end
171172

173+
#
174+
# Stores all cookies of current page in a file.
175+
#
176+
# @return [Integer]
177+
#
178+
# @example
179+
# browser.cookies.store # => Integer
180+
#
181+
def store(path = "cookies.yml")
182+
File.write(path, map(&:to_h).to_yaml)
183+
end
184+
185+
#
186+
# Loads all cookies from the file and sets them for current page.
187+
#
188+
# @return [true]
189+
#
190+
# @example
191+
# browser.cookies.load # => true
192+
#
193+
def load(path = "cookies.yml")
194+
cookies = YAML.load_file(path)
195+
cookies.each { |c| set(c) }
196+
true
197+
end
198+
172199
private
173200

174201
def default_domain

spec/cookies_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,33 @@
262262
expect(browser.body).to_not include("test_cookie")
263263
end
264264
end
265+
266+
describe "#store" do
267+
it "stores cookies" do
268+
page.go_to("/set_cookie")
269+
270+
page.cookies.store("test.yml")
271+
272+
cookies = YAML.load_file("test.yml")
273+
expect(cookies.size).to eq(1)
274+
expect(cookies[0]["name"]).to eq("stealth")
275+
expect(cookies[0]["value"]).to eq("test_cookie")
276+
expect(cookies[0]["domain"]).to eq("127.0.0.1")
277+
ensure
278+
File.delete("test.yml")
279+
end
280+
end
281+
282+
describe "#load" do
283+
it "stores cookies" do
284+
cookie = { "name" => "stealth", "value" => "hello world", "domain" => "127.0.0.1", "path" => "/" }
285+
File.write("test.yml", [cookie].to_yaml)
286+
page.cookies.load("test.yml")
287+
288+
page.go_to("/get_cookie")
289+
expect(page.body).to include("hello world")
290+
ensure
291+
File.delete("test.yml")
292+
end
293+
end
265294
end

0 commit comments

Comments
 (0)