Wednesday, June 15, 2011

How to clear or delete cookies in Selenium


How to delete a cookies in Selenium IDE

It is very much necessary to delete cookies in order to continue the script execution.

There 2 ways to clear

  1. Either clear the browser cache including cookies
  2. Else delete the cookie either by name or all.

Commands used for deleting cookies in Selenium are:

deleteAllVisibleCookies ( )
Calls deleteCookie with recurse=true on all cookies visible to the current page. As noted on the documentation for deleteCookie, recurse=true can be much slower than simply deleting the cookies using a known domain/path.

deleteCookie ( name,optionsString )
Delete a named cookie with specified path and domain. Be careful; to delete a cookie, you need to delete it using the exact same path and domain that were used to create the cookie. If the path is wrong, or the domain is wrong, the cookie simply won't be deleted. Also note that specifying a domain that isn't a subset of the current domain will usually fail. Since there's no way to discover at runtime the original path and domain of a given cookie, we've added an option called 'recurse' to try all sub-domains of the current domain with all paths that are a subset of the current path. Beware; this option can be slow. In big-O notation, it operates in O(n*m) time, where n is the number of dots in the domain name and m is the number of slashes in the path.

Arguments:
1.name - the name of the cookie to be deleted

2. optionsString - options for the cookie. Currently supported options include 'path', 'domain' and 'recurse.' The optionsString's format is "path=/path/, domain=.foo.com, recurse=true". The order of options are irrelevant. Note that specifying a domain that isn't a subset of the current domain will usually fail.


Selenium IDE:


1.      Usage of deleteAllVisibleCookies ( )


<tr>
            <td>deleteAllVisibleCookies</td>
            <td></td>
            <td></td>
</tr>

2.      Usage of deletecookie

Get the specific cookie name you want to delete in FF -> Tools -> Options -> Privacy -> Cookies

Ex:

 Name : PREF
 Domain: .google.com
 Path: /


    <tr>
            <td>deleteCookie</td>
            <td>PREF</td>
            <td>“path=/”, domain=”.google.com”, “recurse=true”</td>
</tr>





Selenium RC

Using below user defined JAVA function

protected void deleteCookie(String cookieName)
{
String cookieDomain =
CTPropertiesManager.getProperty("site.properties", 

"site.cookie.domain");

try
{
//get all cookies
Cookie cookies[] = request.getCookies();

Cookie ctCookie=null;
if (cookies !=null)
{
for(int i=0; i<cookies.length; i++)
{
ctCookie=cookies[i];
if (ctCookie.getName().trim().equals(cookieName))
{
if ( cookieDomain != null )
{
ctCookie.setDomain(cookieDomain);
}

ctCookie.setPath("/ct");
ctCookie.setMaxAge(0);
response.addCookie(ctCookie);
}
}//end for
}//end if cookie
}//end try
catch(Exception e){
CTLogManager.log(e);
}
}//end deleteCookie()



No comments:

Post a Comment