The Mozilla Developer Center cookies examples were very close to what I needed. nsICookieManager.remove seemed perfect. In a FireWatir script it looked like what I needed to read/write cookies was something like this:
def removeCookie(brwsr, host, name, path) cmd = "Components.classes['@mozilla.org/cookiemanager;1']" cmd += ".getService(Components.interfaces.nsICookieManager)" cmd += ".remove(\"" + host + "\", \"" + name + "\", \"" + path + "\", false)"; puts "Removing cookie " + name + " from " + host + " for path " + path result = brwsr.execute_script "#{cmd}\n" end def readCookies(brwsr, site) cmd = "var uri = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService)" cmd += ".newURI(\"" + site + "\", null, null);" cmd += "Components.classes['@mozilla.org/cookieService;1']" cmd += ".getService(Components.interfaces.nsICookieService)" cmd += ".getCookieString(uri, null);" result = brwsr.execute_script "#{cmd}\n" end
In my initial attempts to use this I passed in "http://myhost" and although the method succeeded subsequent logging of the updated cookie string showed no change at all. After some fumbling around I discovered a detailed example of manipulating cookies through javascript in an extension. The main difference in their example was no protocol:// in front of the host/domain name. After that it worked perfectly. A simple example follows:
require "watir" protocol = "http"; host = "mytesthost"; initial_path = "/somethingfuntotest" site = protocol + "://" + host def url(brwsr) return brwsr.js_eval("document.location.toString();") end def readCookies(brwsr, site) cmd = "var uri = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService)" cmd += ".newURI(\"" + site + "\", null, null);" cmd += "Components.classes['@mozilla.org/cookieService;1']" cmd += ".getService(Components.interfaces.nsICookieService)" cmd += ".getCookieString(uri, null);" result = brwsr.execute_script "#{cmd}\n" end #Ref https://developer.mozilla.org/en/nsICookieManager #Ref http://www.springenwerk.com/2007/07/using-cookies-with-firefox-extensions.html #Usage removeCookie(brwsr, "myhostname", "SessionIdCookieName", "/") #Note that passing http://myhostname for host does not appear to work def removeCookie(brwsr, host, name, path) cmd = "Components.classes['@mozilla.org/cookiemanager;1']" cmd += ".getService(Components.interfaces.nsICookieManager)" cmd += ".remove(\"" + host + "\", \"" + name + "\", \"" + path + "\", false)"; puts "Removing cookie " + name + " from " + host + " for path " + path result = brwsr.execute_script "#{cmd}\n" end Watir::Browser.default = "firefox" brwsr = Watir::Browser.new #May have a session id puts "Cookies for " + site + ": " + readCookies(brwsr, site) removeCookie(brwsr, host, "JSESSIONID", "/") #Session id is now gone ... yay! puts "Cookies for " + site + ": " + readCookies(brwsr, site)