Steps To Add Delete and Update cookies using WebDriver in Automation

Posted By : Sachchidanand Tripathi | 15-Jun-2017

Some times we need to handle cookies for testing purpose while testing web services based application.

Steps to create , update and delete cookies are as:


Step 1:

Import cookie class like as:


import  org.openqa.selenium.Cookie;


Step 2

Retrieve all cookies like as:





private  Set<Cookie> getAllCookies() 


    //-------Get all cookies using this line of code -------

       return driver.manage().getCookies();

     //-------Get name based cookie  -----------

    return driver.manage().getCookieNamed(name);

      //-------Get value based cookie  -----------

      return driver.manage().getCookieNamed(name).getValue();



Step 3

Create cookies like as :






//-------Single cookie -----------------------------------------------------------------------

private void addCookie(String name, String value, String domain, String path, Date expiry) {


        new Cookie(name, value, domain, path, expiry));



//-----Multiple cookies creation-------------------------------------------------------

private void AddingCookies(Set<Cookie>  allCookies, String domain) {

        for (Cookie c : allCookies) {

            if (c != null) {

                if (c.getDomain().contains(domain)){


                    new Cookie(name, value, domain, path, expiry));







Step 4

Delete cookies like as :





//This method deletes a specific cookie

private void deleteCookieNamed(String name) {

       //Single cookies


       //All cookies





Request for Proposal

Recaptcha is required.

Sending message..