Cookies in Selenium


Cookies in Selenium


An HTTP cookie is a small piece of data sent from a website and stored in the user's web browser while the user is browsing.
Cookies were designed to record the user's browsing activity.

The following Cookie methods are widely used in selenium:

Add Cookie
Method Name: addCookie(Cookie cookie)
Syntax: driver.Manage().Cookies.addCookie(arg0);
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name of the cookie to add.

Delete Cookie
Method Name: deleteCookie(Cookie cookie)
Syntax: driver.manage()Cookies.deleteCookie(arg0);
Purpose: Delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.
Parameter: Cookie

Delete Cookie with Name
Method Name: deleteCookieNamed(java.lang.String name)
Syntax: driver.manage().Cookies.deleteCookieNamed(arg0);
Purpose: Delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.
Parameters: name - The name of the cookie to delete

Delete All Cookies
Method Name: deleteAllCookies()
Syntax: driver.manage().Cookies.deleteAllCookies();
Purpose: It will delete all the cookies for the current domain.
Parameters: N/A

Get Cookies
Method Name: getCookies()
Syntax: driver.manage().Cookies.getCookies();
Purpose: Get all the cookies for the current domain..
Returns: A Set of cookies for the current domain.

Get the Cookie with Specific Name
Method Name: getCookieNamed(java.lang.String name)
Syntax: driver.manage().Cookies.getCookieNamed(arg0);
Purpose: To Get a cookie with a given name.
Parameters: name - the name of the cookie
Returns: It will return the cookie value for the name specified, or null if no cookie found with the given name


In our Next Tutorial let us learn to capture screenshot in selenium!

No comments:

Post a Comment