How to handle Cookies when performing Selenium JUnit testing?
Hi Ian!
In order to handle Cookies when performing Selenium JUnit testing, you will need to add the below code snippet into your automation scripts.
driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie
Set<Cookie> cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies
for (Cookie itemCookie : cookiesSet) {
System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";"
+ itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure()));
}
driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name
driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie
driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name
driver.manage().deleteAllCookies(); // Deletes all the cookies
Here is the GitHub repo for the same
Hope this helps!