|
|
|
#http://groups.google.com/group/watir-general/browse_thread/thread/d5d420fd56147501 |
|
module FireWatir |
|
class Firefox |
|
|
|
# - gets all cookies from browser |
|
def getAllCookies |
|
jssh_command = "var comp = #{WINDOW_VAR}.Components; var cookieMgr = |
|
comp.classes[\"@mozilla.org/cookiemanager; |
|
1\"].getService(comp.interfaces.nsICookieManager);" |
|
jssh_command += "var allcookies=''; for (var e = cookieMgr.enumerator; |
|
e.hasMoreElements();) { var cookie = |
|
e.getNext().QueryInterface(comp.interfaces.nsICookie); " |
|
jssh_command += "allcookies = allcookies + cookie.host + \";\" + |
|
cookie.name + \"='\" + cookie.value + \"';Path=\" +cookie.path + |
|
\";isSecure=\" + cookie.isSecure + " |
|
jssh_command += "\";Expires=\" + cookie.expires + \";P3P=\" + |
|
cookie.status + \";Policy=\" + cookie.policy + \"\\n\"; } allcookies;" |
|
$jssh_socket.send("#{jssh_command}\n", 0) |
|
val = read_socket() |
|
vals = val.split("\n") |
|
return vals |
|
end |
|
|
|
#- getSessionCookies |
|
def getSessionCookies |
|
jssh_command = "var comp = #{WINDOW_VAR}.Components; var cookieMgr = |
|
comp.classes[\"@mozilla.org/cookiemanager; |
|
1\"].getService(comp.interfaces.nsICookieManager);" |
|
jssh_command += "var allcookies=''; for (var e = cookieMgr.enumerator; |
|
e.hasMoreElements();) { var cookie = |
|
e.getNext().QueryInterface(comp.interfaces.nsICookie); " |
|
jssh_command += "if (cookie.expires == 0) { allcookies = allcookies + |
|
cookie.host + \";\" + cookie.name + \"='\" + cookie.value +\"';Path=\" |
|
+cookie.path + \";isSecure=\" + cookie.isSecure + " |
|
jssh_command += "\";Expires=\" + cookie.expires + \";P3P=\" + |
|
cookie.status + \";Policy=\" + cookie.policy + \"\\n\"; } } |
|
allcookies;" |
|
$jssh_socket.send("#{jssh_command}\n", 0) |
|
val = read_socket() |
|
vals = val.split("\n") |
|
return vals |
|
end |
|
|
|
# - get persistentCookies |
|
def getPersistentCookies |
|
jssh_command = "var comp = #{WINDOW_VAR}.Components; var cookieMgr = |
|
comp.classes[\"@mozilla.org/cookiemanager; |
|
1\"].getService(comp.interfaces.nsICookieManager);" |
|
jssh_command += "var allcookies=''; for (var e = cookieMgr.enumerator; |
|
e.hasMoreElements();) { var cookie = |
|
e.getNext().QueryInterface(comp.interfaces.nsICookie); " |
|
jssh_command += "if (cookie.expires != 0) { allcookies = allcookies + |
|
cookie.host + \";\" + cookie.name + \"='\" + cookie.value +\"';Path=\" |
|
+cookie.path + \";isSecure=\" + cookie.isSecure + " |
|
jssh_command += "\";Expires=\" + cookie.expires + \";P3P=\" + |
|
cookie.status + \";Policy=\" + cookie.policy + \"\\n\"; } } |
|
allcookies;" |
|
$jssh_socket.send("#{jssh_command}\n", 0) |
|
val = read_socket() |
|
vals = val.split("\n") |
|
return vals |
|
end |
|
|
|
# - getCookie by Name or by Name and domain |
|
def getCookie(cookiename, domain = nil) |
|
cookiename = cookiename.upcase |
|
jssh_command = "var comp = #{WINDOW_VAR}.Components; var cookieMgr = |
|
comp.classes[\"@mozilla.org/cookiemanager; |
|
1\"].getService(comp.interfaces.nsICookieManager);" |
|
jssh_command += "var allcookies=''; for (var e = cookieMgr.enumerator; |
|
e.hasMoreElements();) { var cookie = |
|
e.getNext().QueryInterface(comp.interfaces.nsICookie); " |
|
if domain.nil? |
|
jssh_command += "if (cookie.name.toUpperCase() == \"#{cookiename}\") |
|
{ allcookies = allcookies + cookie.host + \";\" + cookie.name + \"=\" |
|
+ cookie.value +\";Path=\" +cookie.path + \";isSecure=\" + |
|
cookie.isSecure + " |
|
else |
|
jssh_command += "if (cookie.name.toUpperCase() == \"#{cookiename}\" && |
|
cookie.host == \"#{domain}\") { allcookies = allcookies + cookie.host |
|
+ \";\" + cookie.name + \"=\" + cookie.value +\";Path=\" +cookie.path |
|
+ \";isSecure=\" + cookie.isSecure + " |
|
end |
|
jssh_command += "\";Expires=\" + cookie.expires + \";P3P=\" + |
|
cookie.status + \";Policy=\" + cookie.policy + \"\\n\"; } } |
|
allcookies;" |
|
$jssh_socket.send("#{jssh_command}\n", 0) |
|
val = read_socket() |
|
vals = val.split("\n") |
|
return vals |
|
end |
|
|
|
# - getCookies By a particular domain/host |
|
def getCookiesByDomain(domain) |
|
if (!domain.nil? && domain[0] != ".") |
|
domain = "." + domain |
|
end |
|
vals = [] |
|
i=0 |
|
while (i<2) |
|
jssh_command = "var comp = #{WINDOW_VAR}.Components; var cookieMgr = |
|
comp.classes[\"@mozilla.org/cookiemanager; |
|
1\"].getService(comp.interfaces.nsICookieManager);" |
|
jssh_command += "var allcookies=''; for (var e = cookieMgr.enumerator; |
|
e.hasMoreElements();) { var cookie = |
|
e.getNext().QueryInterface(comp.interfaces.nsICookie); " |
|
jssh_command += "if (cookie.host == \"#{domain}\") { allcookies = |
|
allcookies + cookie.host + \";\" + cookie.name + \"=\" + cookie.value + |
|
\";Path=\" +cookie.path + \";isSecure=\" + cookie.isSecure + " |
|
jssh_command += "\";Expires=\" + cookie.expires + \";P3P=\" + |
|
cookie.status + \";Policy=\" + cookie.policy + \"\\n\"; } } |
|
allcookies;" |
|
$jssh_socket.send("#{jssh_command}\n", 0) |
|
val = read_socket() |
|
vals.concat (val.split("\n")) |
|
i=i+1 |
|
domain = domain[1..domain.length] |
|
end |
|
return vals |
|
end |
|
|
|
end |
|
end |