Class: Watir::Cookies
- Inherits:
-
Object
- Object
- Watir::Cookies
- Defined in:
- lib/watir/cookies.rb
Instance Method Summary collapse
-
#[](name) ⇒ Hash
Returns a cookie by name.
-
#add(name, value, opts = {}) ⇒ Object
Adds new cookie.
-
#clear ⇒ Object
Deletes all cookies.
-
#delete(name) ⇒ Object
Deletes cookie by given name.
-
#initialize(control) ⇒ Cookies
constructor
A new instance of Cookies.
-
#load(file = '.cookies') ⇒ Object
Load cookies from file.
-
#save(file = '.cookies') ⇒ Object
Save cookies to file.
-
#to_a ⇒ Array<Hash>
Returns array of cookies.
Constructor Details
#initialize(control) ⇒ Cookies
Returns a new instance of Cookies.
7 8 9 |
# File 'lib/watir/cookies.rb', line 7 def initialize(control) @control = control end |
Instance Method Details
#[](name) ⇒ Hash
Returns a cookie by name.
38 39 40 |
# File 'lib/watir/cookies.rb', line 38 def [](name) to_a.find { |c| c[:name] == name.to_s } end |
#add(name, value, opts = {}) ⇒ Object
Adds new cookie.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/watir/cookies.rb', line 60 def add(name, value, opts = {}) = { name: name, value: value } [:secure] = opts[:secure] if opts.key?(:secure) [:http_only] = opts[:http_only] if opts.key?(:http_only) [:path] = opts[:path] if opts.key?(:path) [:same_site] = opts[:same_site] if opts.key?(:same_site) expires = opts[:expires] if expires [:expires] = expires.is_a?(String) ? ::Time.parse(expires) : expires end [:domain] = opts[:domain] if opts.key?(:domain) @control. end |
#clear ⇒ Object
Deletes all cookies.
98 99 100 |
# File 'lib/watir/cookies.rb', line 98 def clear @control. end |
#delete(name) ⇒ Object
Deletes cookie by given name.
87 88 89 |
# File 'lib/watir/cookies.rb', line 87 def delete(name) @control.(name) end |
#load(file = '.cookies') ⇒ Object
Load cookies from file
124 125 126 127 128 |
# File 'lib/watir/cookies.rb', line 124 def load(file = '.cookies') YAML.safe_load_file(file, permitted_classes: [::Symbol, ::Time]).each do |c| add(c.delete(:name), c.delete(:value), c) end end |
#save(file = '.cookies') ⇒ Object
Save cookies to file
111 112 113 |
# File 'lib/watir/cookies.rb', line 111 def save(file = '.cookies') File.write(file, to_a.to_yaml) end |
#to_a ⇒ Array<Hash>
Returns array of cookies.
21 22 23 24 25 |
# File 'lib/watir/cookies.rb', line 21 def to_a @control..map do |e| e.merge(expires: e[:expires]&.to_time) end end |