Class: Watir::Cookies
- Inherits:
-
Object
- Object
- Watir::Cookies
- Defined in:
- lib/watir-webdriver/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.
6 7 8 |
# File 'lib/watir-webdriver/cookies.rb', line 6 def initialize(control) @control = control end |
Instance Method Details
#[](name) ⇒ Hash
Returns a cookie by name.
37 38 39 |
# File 'lib/watir-webdriver/cookies.rb', line 37 def [](name) to_a.find { |c| c[:name] == name.to_s } end |
#add(name, value, opts = {}) ⇒ Object
Adds new cookie.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/watir-webdriver/cookies.rb', line 56 def add(name, value, opts = {}) = { name: name, value: value, secure: opts[:secure], path: opts[:path], expires: opts[:expires], domain: opts[:domain] } @control. end |
#clear ⇒ Object
Deletes all cookies.
89 90 91 |
# File 'lib/watir-webdriver/cookies.rb', line 89 def clear @control. end |
#delete(name) ⇒ Object
Deletes cookie by given name.
78 79 80 |
# File 'lib/watir-webdriver/cookies.rb', line 78 def delete(name) @control.(name) end |
#load(file = '.cookies') ⇒ Object
Load cookies from file
115 116 117 118 119 |
# File 'lib/watir-webdriver/cookies.rb', line 115 def load(file = '.cookies') YAML.load(IO.read(file)).each do |c| add(c.delete(:name), c.delete(:value), c) end end |
#save(file = '.cookies') ⇒ Object
Save cookies to file
102 103 104 |
# File 'lib/watir-webdriver/cookies.rb', line 102 def save(file = '.cookies') IO.write(file, to_a.to_yaml) end |
#to_a ⇒ Array<Hash>
Returns array of cookies.
20 21 22 23 24 |
# File 'lib/watir-webdriver/cookies.rb', line 20 def to_a @control..map do |e| e.merge(expires: e[:expires] ? to_time(e[:expires]) : nil) end end |