We get information out of cookies, which are solely comprised of that. It contains information about the user and their preference, passwords, and other stuff.
Data are stored in a key pair value. Cookies are small pieces of data sent from a web application, stored in a web browser while the user browses. Selenium is an automation tool solely made to automate web applications and has the method to interact with cookies.
driver.manage().getCookies();
Return List of all Cookies
driver.manage().getCookieNamed(arg0);
Return specific cookies by name.
driver.manage().addCookie(arg0);
Add the cookie by adding
driver.manage().deleteCookie(arg0);
Delete a cookie
driver.manage().deleteCookieNamed(arg0);
Delete cookie with a specified Name
driver.manage().deleteAllCookies();
Delete all cookies
Why Handle?
- Cookies are needed to be handled by testers as they test the web application.
- When the need to log in every time is low, cookies can help the tester ease that pain.
- If it is not stored, the user must log in every time.
- Increases coding effort and time of execution.
Example:
Let us play around with cookies using selenium.
getCookie
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("http://www.programsbuzz.com/user/login");
driver.manage().window().maximize();
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie : cookies) {
String cName = cookie.getName();
String cValue = cookie.getValue();
System.out.println(cName + cValue);
}
}
}
- We can see we have fetched the name and value of the cookie iterated in the advanced loop.
getCookieNamed
System.out.println(driver.manage().getCookieNamed("_gcl_au"));

We can see here the information about the cookie being printed.
addCookie
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("http://www.programsbuzz.com/user/login");
driver.manage().window().maximize();
Set<Cookie> cookies = driver.manage().getCookies();
Cookie cobj = new Cookie("mycookie", "1234567890");
driver.manage().addCookie(cobj);
for (Cookie cookie : cookies)
{
System.out.println(cookie.getName() + " : " + cookie.getValue());
}
}
}
- Doing this will add the cobj cookies to the present site with the provided data.
- Once the driver has been closed, it resets and reverts to the default size.
deleteCookie
driver.manage().deleteCookie(cobj)
This will delete our custom cookie from the previous step contained in cobj.
deleteAllCookies
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("http://www.programsbuzz.com/user/login");
driver.manage().window().maximize();
Set<Cookie> cookies = driver.manage().getCookies();
System.out.println(cookies.size());
driver.manage().deleteAllCookies();
Set<Cookie> postCookies = driver.manage().getCookies();
System.out.println(postCookies.size());
}
}
- Deletes all cookies after loading.