Using deleteCookieNamed() and deleteCookie() to delete cookies in selenium WebDriver

Earlier we learnt how to add cookie and get cookie details using different methods like addCookie(), getCookieNamed() and getCookies(). Sometimes you also need to delete cookies in selenium. Selenium delete cookies can be done using deleteCookieNamed() or passing cookie object in deleteCookie() method.
Let's see how to use deleteCookieNamed() and deleteCookie() to delete cookies in selenium test.

Selenium delete cookies example

package TestPack;

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

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

public class deleteCookie {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("webdriver.chrome.driver", exePath);
  WebDriver driver = new ChromeDriver();
  
  driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
  
  //Add cookie with name, value, domain, path, expiry, isSecure and isHTTPOnly.
  driver.manage().addCookie(new Cookie("Cookiename1", "cookieval1", ".only-testing-blog.blogspot.com", "/", new Date((2035-1900),05,07), false, true));
  Cookie cookieobj = new Cookie("Cookiename2", "cookieval2", ".only-testing-blog.blogspot.com", "/", new Date((2035-1900),03,06), false, true);
  driver.manage().addCookie(cookieobj);
  driver.manage().addCookie(new Cookie("Cookiename3", "cookieval3", ".only-testing-blog.blogspot.com", "/", 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 and print.
  Iterator value = cookies.iterator();   
  while (value.hasNext()) { 
         System.out.println(value.next()); 
  }
  
  //Delete cookie by name
  driver.manage().deleteCookieNamed("Cookiename1");
  //Deleting cookie using deleteCookie
  driver.manage().deleteCookie(cookieobj);
  
  System.out.println("\n");
  System.out.println("********** Cookies after deleting cookies by name and cookie object of current browsing context **************");
  System.out.println("\n");
    
  //Get all cookie details from browser after deleting cookie by name and deleteCookie() method. 
  Set<Cookie> cookies1 = driver.manage().getCookies();
      
  //Iterate through cookies to isolate each cookie and print.
  Iterator value1 = cookies1.iterator();   
  while (value1.hasNext()) { 
         System.out.println(value1.next()); 
  }
 }
}
Output :
Cookiename1=cookieval1; expires=Thu, 07 Jun 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;
Cookiename2=cookieval2; expires=Fri, 06 Apr 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;
Cookiename3=cookieval3; expires=Fri, 06 Apr 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;


********** Cookies after deleting cookies by name and cookie object **************


Cookiename3=cookieval3; expires=Fri, 06 Apr 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;


Here, you can see that we had used deleteCookieNamed() method to delete cookie by name Cookiename1. deleteCookie() method has used cookie object cookieobj to delete that specific cookie of current browsing context. This way you can delete cookies in selenium.

1 comment:

  1. Thanks for sharing article on selenium webdriver it is very good information

    ReplyDelete