Class: Watir::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/tagen/watir.rb

Instance Method Summary collapse

Instance Method Details

#dump_cookies(file) ⇒ Object

Write cookies to Mozilla cookies.txt-style IO stream

Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tagen/watir.rb', line 55

def dump_cookies(file)
  io = case file
  when String
    open(file, "w")
  else
    file
  end

  cookies.to_a.each do |cookie|
    io.puts([
      cookie[:domain],
      "FALSE", # for_domain
      cookie[:path],
      cookie[:secure] ? "TRUE" : "FALSE",
      cookie[:expires].to_i.to_s,
      cookie[:name],
      cookie[:value]
    ].join("\t"))
  end

  io.close if String === file

  self
end

#keep_cookies!Object

TODO: support windows.

firefox only.



9
10
11
12
13
14
# File 'lib/tagen/watir.rb', line 9

def keep_cookies!
  require "tagen/selenium-webdriver/webdriver/firefox"

  launcher = driver.instance_variable_get("@bridge").instance_variable_get("@launcher")
  launcher.keep_cookies!
end

#load_cookies(file) ⇒ Object

Read cookies from Mozilla cookies.txt-style IO stream

Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tagen/watir.rb', line 20

def load_cookies(file)
  now = ::Time.now

  io = case file
  when String
    open(file)
  else
    file
  end

  io.each_line do |line|
    line.chomp!
    line.gsub!(/#.+/, '')
    fields = line.split("\t")

    next if fields.length != 7

    name, value, domain, for_domain, path, secure, version = fields[5], fields[6], 
      fields[0], (fields[1] == "TRUE"), fields[2], (fields[3] == "TRUE"), 0

    expires_seconds = fields[4].to_i
    expires = (expires_seconds == 0) ? nil : ::Time.at(expires_seconds)
    next if expires and (expires < now)

    cookies.add(name, value, domain: domain, path: path, expires: expires, secure: secure)
  end

  io.close if String === file

  self
end