What are cookies
Cookies are text files stored on your computer, that contain information sites need to remember of your visit on them, like some of your preferences that the sites will persist while browsing their pages (for example: the language you are viewing the site in, the username you logged in with, the contents of your shopping cart, and so on). Cookies are used by your browser at the time you access the site. Each cookie has a name and a value, but also a scope, determined by the domain and path properties of the cookie (these are used to determine the site they belong to). A website can only set cookies valid on its’ domain and subdomains, not domains that do not belong to it. An expiry date can also be assigned to the cookie.
There are several types of cookies: session cookies are assigned to a website and are only valid while the site is being accessed. After leaving the site or closing the browser, these objects are deleted. Also, persistent cookies are used, those cookies that have an expiration date and that are valid beyond closing the browser.
Cookie objects supported by WebDriver
In the org.openqa.selenium module from the selenium-api library, you will find a class that emulates cookie objects, named of course, Cookie. A Cookie object can at most have the following attributes, as defined in this class: name; value; path; domain; expiry; isSecure. A Cookie object must have at least two attributes, namely name and value.
The Cookie object offers several constructors to build your cookie object, having the need to pass in the following parameters:
- Minimal constructor with parameters: name, value. Example of creating such a Cookie object:
Cookie cookie = new Cookie("testCookie", "testValue");
- name, value, path
- name, value, path, expiry
- name, value, path, expiry, domain
- name, value, path, expiry, domain, isSecure
Working with cookies in WebDriver
All methods related to cookies in WebDriver are accessible from the Options interface. First you must declare a variable for the webdriver, for example:
private WebDriver driver;
Then, you can use the cookie methods as described below:
- Get all cookies: After opening a page whose cookies you want to find, use:
You can save these values in a set, to use them later on:
Set<Cookie> cookies = driver.manage().getCookies();
- Get a certain cookie: When you know the name of the cookie whose properties you want to get, you can use:
Storing the properties of this cookie is done by assigning the result of the above method to a Cookie object:
Cookie theCookie = driver.manage().getCookieNamed("cookieName");
- Deleting all the cookies on a page is done easily, by using:
- Deleting only a particular cookie, whose name you know, is done by using:
- Adding a new cookie: To add a new cookie to the browser, first you must create a Cookie object with all its’ needed properties. Afterwards, just use the following method:
After adding the cookie, for it to be applied to the page you opened, a page refresh must be done right after adding it.
Tip: In case you are not sure how to add the ‘domain’ property to a cookie, but you know what page it must be active on, you can: initially open that page; delete the cookie from the page, if it exists; create the Cookie object without setting the ‘domain’ property; add the cookie to the already opened page; refresh the page. The domain will be added automatically.
- Editing cookies: You cannot directly edit the properties of a cookie. Instead, what you can do is: delete the existing cookie from the browser; create a new Cookie variable to reflect the new values you want for its’ properties ; add the new cookie to the browser; refresh the page.