What is a Cookie?
A cookie is a small piece of data that is sent from a website and stored on your computer. Cookies are mostly used to recognize the user and load the stored information. Using Selenium WebDriver we can easily pass the cookie to the domain. Each cookie is associated with a name, value, domain, path, expiry, and the status of whether it is secure or not. In order to validate a client, a server parses all of these values in a cookie. Handling cookies during your automated testing is a common task since most websites are using cookies to track and retain specific user information.
Why do I need to handle cookies during automated testing?
Automated test runs may encounter a possible issue. It is possible that the website being tested is using the same cookie to store particular data for several tests.
Let’s take an example where you are adding an item to a shopping cart to test it. If one test adds the item to the cart, data might be saved in a cookie.
It’s possible that the logic in the second test believes the cart is empty. However, the second test fails since the cookie from the previous test is still saved.
In Selenium WebDriver, we can query and interact with cookies with the below built-in methods.
Various cookie operations
Create and add the cookie
The add_cookie() method takes a cookie dictionary as its parameter, focusing on the most important attributes – name and value.
driver.manage().addCookie(arg0);
Get Named Cookie
It returns the serialized cookie data matching the cookie name among all associated cookies.
driver.manage().getCookieNamed(arg0);
Get All Cookies
It returns a ‘successful serialized cookie data’ for the current browsing context. If the browser is no longer available, it returns an error.
driver.manage().getCookies();
Delete specific cookie
It deletes the cookie data matching the provided cookie name.
driver.manage().deleteCookieNamed(arg0);
Delete all cookies
It deletes all the cookies of the current browsing context.
driver.manage().deleteAllCookies();
Let us see an example where we add cookies, get the details of the cookies, delete a particular cookie, and delete all the cookies. I’m using Selenium – 4.15.0.
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.Set;
public class Cookie_Demo {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximize");
ChromeDriver driver = new ChromeDriver(options);
String URL = "http://google.com/";
driver.navigate().to(URL);
// Add the cookies into current browser context
driver.manage().addCookie(new Cookie("mycookie1", "123456789123"));
driver.manage().addCookie(new Cookie("mycookie2", "we34rf125ty6"));
Cookie cookie1 = new Cookie("mycookie3", "ABCDEFGH");
driver.manage().addCookie(cookie1);
// Get All available cookies
System.out.println("List of Cookies");
Set<Cookie> cookiesList = driver.manage().getCookies();
for (Cookie getcookies : cookiesList) {
System.out.println(getcookies);
}
// Get cookie details with named cookie 'foo'
System.out.println("Get detail of 2nd cookie");
Cookie cookie2 = driver.manage().getCookieNamed("mycookie2");
System.out.println(cookie2);
// delete a cookie with name 'mycookie2'
System.out.println("Delete 2nd cookie");
driver.manage().deleteCookieNamed("mycookie2");
// deletes all cookies
System.out.println("Delete all cookies");
driver.manage().deleteAllCookies();
driver.quit();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/11/image-107.png?w=1200)
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!