removeCookie

The following action removes cookie with a given name, belonging to a given path:


// The following examples try to give an indication of how Selenium can be extended with javascript.
function createCookie(doc, name,value, path,days)
{
        if (!path) {
            path = "/";        
        }
    
        if (days)
        {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        doc.cookie = name+"="+value+expires+"; path="+path;
}

/*
 * Removes the cookie with the given name.
 *  text - the cookie name
 *  path - the cookie path
 */
Selenium.prototype.doRemoveCookie = function(text, path) {    
    createCookie(this.page().currentDocument, text, "", path, -1);
};			
			

I found the code for the createCookie on the Selenium forums. I just added a member function on the Selenium object prototype named doRemoveCookie which in effect creates a Selenium action 'removeCookie'.

Example:

removeCookie JSESSIONID /admin
removeCookie JSESSIONID /

In the example above, I call the removeCookie twice, one removing a JSESSIONID cookie set on the root (/) and one removing a JSESSIONID cookie set on the directory /admin. In some cases, I found that it would be relevant to have to give the path information as some servers set cookies with the same name on different paths.

notes

  • For more information about usage, visit my hompepage and my blog.

File: 

AttachmentSize
Plain text icon removeCookie.txt782 bytes

Type: 

User-Extension