Using deleteAllCookies() to Delete all cookies in Selenium WebDriver

Earlier we already learn different selenium webdriver methods to add cookie, get cookies detail by name and all cookies. Sometimes you also need to delete all cookies in selenium webdriver test. You may experience where website is asking for permission to run and use cookies when you first time visit the site. Cookies is piece of experience which helps user to retain login, search history and other details for better user experience. Some time you need to clear all cookies in selenium test and you need selenium webdriver command to delete all cookies in the browser. Selenium Webdriver have built in deleteAllCookies() to Delete all cookies from current running browser window in test.

Let's see how to use deleteAllCookies() to Delete all cookies in selenium.

Deleting all cookies in selenium using deleteAllCookies() in Selenium WebDriver
package TestPack;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

public class deleteAllCookies {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("", exePath);
  WebDriver driver = new ChromeDriver();
  //Add cookie with name, value, domain, path, expiry, isSecure and isHTTPOnly.
  driver.manage().addCookie(new Cookie("Cookiename1", "cookieval1", "", "/", new Date((2035-1900),05,07), false, true));
  driver.manage().addCookie(new Cookie("Cookiename2", "cookieval2", "", "/", new Date((2035-1900),02,06), false, true));
  driver.manage().addCookie(new Cookie("Cookiename3", "cookieval3", "", "/", new Date((2035-1900),03,06), false, true));
  //Get all cookie details from browser using getCookies().  
  Set<Cookie> cookies = driver.manage().getCookies();
  //Iterate through cookies to isolate each cookie.
  Iterator value = cookies.iterator();   
  while (value.hasNext()) { 
  //Delete all cookies in selenium using deleteAllCookies.
  System.out.println("********** Check if any cookie present after deleting all cookies **************");
  //Get all cookie details from browser. 
  Set<Cookie> cookies1 = driver.manage().getCookies();
  //Check if set has any value.
  if(!cookies1.isEmpty()) {
  //Iterate through cookies to isolate each cookie.
  Iterator value1 = cookies1.iterator();   
  while (value1.hasNext()) { 
   System.out.println("Set is empty. No cookie found.");
Output :
Cookiename1=cookieval1; expires=Thu, 07 Jun 2035 12:00:00 IST; path=/;;secure;
Cookiename2=cookieval2; expires=Tue, 06 Mar 2035 12:00:00 IST; path=/;;secure;
Cookiename3=cookieval3; expires=Fri, 06 Apr 2035 12:00:00 IST; path=/;;secure;

********** Check if any cookie present after deleting all cookies **************

Set is empty. No cookie found.
In above example you can see that deleteAllCookies() has cleared all cookies in selenium we set earlier using addCookie() for current browsing context.

No comments:

Post a Comment