How to Manage Cookies in Selenium WebDriver
In this article we will see the functions to manage cookie of a browser.
In here I am using full c# code, I will provide JAVA code later on.
First step should be declaring the driver
private IWebDriver driver = new FirefoxDriver();
Please follow the your necessary functions from my past posts. Let's start our topic on cookie.
-To create a cookie object in c#
private Cookie myCookie = new Cookie();
There are 4 ways to create a cookie object (to give parameter)
1. (name, value)
2. (name, value, path)
3. (name, value, path,expiry)
4. (name, value, domain, path,expiry)
Parameters:
name: The name of the cookie.(string type)
value: The value of the cookie.(string type)
path:The path of the cookie.(string type)
domain:The domain of the cookie. (string type)
expiry:The expiration date of the cookie.(DateTime structure type)
Note : A cookie is Serializable(can be stored)
I use following way to declear a cookie
private Cookie myCookie = new Cookie("name", "value", "Domain", "Path", expDate);
Note : for expDate, I use static to use it all around
private static DateTime expDate = new DateTime(2012,12,25);
-To add our own cookie(in here myCookie)
driver.Manage().Cookies.AddCookie(myCookie);
-To Delete all cookie
driver.Manage().Cookies.DeleteAllCookies();
-To Delete one Specific cookie(in here myCookie)
driver.Manage().Cookies.DeleteCookie(myCookie);
-To Delete one Specific cookie whose name(string) is "shantonu" driver.Manage().Cookies.DeleteCookieNamed("shantonu");
-To Get the cookie whose name(string) is "shantonu" driver.Manage().Cookies.GetCookieNamed("Shantonu");
Note : This will give us an Cookie type object as output
-To know how many cookie is present
driver.Manage().Cookies.AllCookies.Count;
Note : This will give us an integer(the number of cookie) as output
-To get the Index value of one Specific cookie(in here myCookie) driver.Manage().Cookies.AllCookies.IndexOf(myCookie);
Note : This will give us an integer(Zero Based Index Value) as output.
-To Check one Specific cookie(in here myCookie) is present or not driver.Manage().Cookies.AllCookies.Contains(myCookie);
Note : This will give bool result ( true or false)
-To Get/set all cookies defined for the current page.
driver.Manage().Cookies.AllCookies;
Note : This will give you(or you have to provide) a collection of cookie objects from current page.
From myCookie object we can get some information through its properties.
-To Get the domain(as string) of the cookie.
myCookie.Domain;
-To Get the expiration date(as DateTime) of the cookie.
myCookie.Expiry.Value;
-To Get the Name(as string) of the cookie.
myCookie.Name;
-To Get the Path(as string) of the cookie
myCookie.Path;
-To Get a value(bool) to know whether the cookie is secure.
myCookie.Secure;
-To Get the Value(as string) of the cookie.
st = myCookie.Value;
...Thanks...:)
In here I am using full c# code, I will provide JAVA code later on.
First step should be declaring the driver
private IWebDriver driver = new FirefoxDriver();
Please follow the your necessary functions from my past posts. Let's start our topic on cookie.
-To create a cookie object in c#
private Cookie myCookie = new Cookie(
There are 4 ways to create a cookie object (to give parameter)
1. (name, value)
2. (name, value, path)
3. (name, value, path,expiry)
4. (name, value, domain, path,expiry)
Parameters:
name: The name of the cookie.(string type)
value: The value of the cookie.(string type)
path:The path of the cookie.(string type)
domain:The domain of the cookie. (string type)
expiry:The expiration date of the cookie.(DateTime structure type)
Note : A cookie is Serializable(can be stored)
I use following way to declear a cookie
private Cookie myCookie = new Cookie("name", "value", "Domain", "Path", expDate);
Note : for expDate, I use static to use it all around
private static DateTime expDate = new DateTime(2012,12,25);
-To add our own cookie(in here myCookie)
driver.Manage().Cookies.AddCookie(myCookie);
-To Delete all cookie
driver.Manage().Cookies.DeleteAllCookies();
-To Delete one Specific cookie(in here myCookie)
driver.Manage().Cookies.DeleteCookie(myCookie);
-To Delete one Specific cookie whose name(string) is "shantonu" driver.Manage().Cookies.DeleteCookieNamed("shantonu");
-To Get the cookie whose name(string) is "shantonu" driver.Manage().Cookies.GetCookieNamed("Shantonu");
Note : This will give us an Cookie type object as output
-To know how many cookie is present
driver.Manage().Cookies.AllCookies.Count;
Note : This will give us an integer(the number of cookie) as output
-To get the Index value of one Specific cookie(in here myCookie) driver.Manage().Cookies.AllCookies.IndexOf(myCookie);
Note : This will give us an integer(Zero Based Index Value) as output.
-To Check one Specific cookie(in here myCookie) is present or not driver.Manage().Cookies.AllCookies.Contains(myCookie);
Note : This will give bool result ( true or false)
-To Get/set all cookies defined for the current page.
driver.Manage().Cookies.AllCookies;
Note : This will give you(or you have to provide) a collection of cookie objects from current page.
From myCookie object we can get some information through its properties.
-To Get the domain(as string) of the cookie.
myCookie.Domain;
-To Get the expiration date(as DateTime) of the cookie.
myCookie.Expiry.Value;
-To Get the Name(as string) of the cookie.
myCookie.Name;
-To Get the Path(as string) of the cookie
myCookie.Path;
-To Get a value(bool) to know whether the cookie is secure.
myCookie.Secure;
-To Get the Value(as string) of the cookie.
st = myCookie.Value;
...Thanks...:)