Class: Watir::Cookies

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir-classic/cookies.rb

Overview

Returned by Browser#cookies.

Instance Method Summary collapse

Constructor Details

#initialize(page_container) ⇒ Cookies

Returns a new instance of Cookies.



8
9
10
# File 'lib/watir-classic/cookies.rb', line 8

def initialize(page_container)
  @page_container = page_container
end

Instance Method Details

#add(name, value, options = {}) ⇒ Object

Add a cookie.

Examples:

Add a cookie with default options:

browser.cookies.add "name", "value'

Add a cookie with options:

browser.cookie.add "name", "value", :expires => Time.now, :secure => true, :path => "/foo/bar"

Parameters:

  • name (String)

    name of the cookie.

  • value (String)

    value of the cookie.

  • options (Hash) (defaults to: {})

    options for the cookie.

Options Hash (options):

  • :expires (Time)

    Expiration time.

  • :secure (Boolean) — default: false

    Secure flag. Set when value is true.

  • :path (String)

    Path for cookie.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/watir-classic/cookies.rb', line 42

def add(name, value, options={})
  options = options.map do |option|
    k, v = option
    if k == :expires
      "#{k}=#{v.gmtime.strftime("%a, %d %b %Y %H:%M:%S UTC")}"
    elsif k == :secure
      "secure" if v
    else
      "#{k}=#{v}"
    end
  end.compact.join("; ")

  options = "; #{options}" unless options.empty?
  @page_container.document.cookie = "#{name}=#{value}#{options}" 
end

#clearObject

Delete all cookies for the page.



101
102
103
# File 'lib/watir-classic/cookies.rb', line 101

def clear
  each {|cookie| delete cookie[:name]}
end

#delete(name) ⇒ Object

Note:

does not raise any exceptions when cookie with the specified name is not found.

Delete a cookie.

Parameters:

  • name (String)

    Cookie with the specified name to be deleted.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/watir-classic/cookies.rb', line 63

def delete(name)
  options = {:expires => ::Time.now - 60 * 60 * 24}
  delete_with_options name, options

  # make sure that the cookie gets deleted
  # there's got to be some easier way to do this
  uri = URI.parse(@page_container.url)
  domain = uri.host
  return unless domain

  paths = uri.path.split("/").reduce([]) do |paths, path|
    paths << "#{paths.last}/#{path}".squeeze("/")
  end << "/"

  subdomains = domain.split(".").reverse.reduce([]) do |subdomains, part|
    subdomain = "#{part}#{subdomains.last}"
    subdomain = "." + subdomain unless subdomain == domain
    subdomains << subdomain
  end

  subdomains.each do |subdomain|
    domain_options = options.merge :domain => subdomain
    delete_with_options name, domain_options
    delete_with_options name, domain_options.merge(:secure => true)

    paths.each do |path|
      path_options = options.merge :path => path
      delete_with_options name, path_options 
      delete_with_options name, path_options.merge(:secure => true)

      path_domain_options = domain_options.merge :path => path
      delete_with_options name, path_domain_options 
      delete_with_options name, path_domain_options.merge(:secure => true)
    end
  end
end

#each {|cookie| ... } ⇒ Object

Iterate over each cookie.

Examples:

browser.cookies.each do |cookie|
  puts cookie[:name]
  puts cookie[:value]
end

Yield Parameters:

  • cookie (Hash)

    name and value pair of the cookie.



21
22
23
24
25
26
# File 'lib/watir-classic/cookies.rb', line 21

def each
  @page_container.document.cookie.split(";").each do |cookie|
    name, value = cookie.strip.split("=")
    yield({:name => name, :value => value})
  end
end