How to Add, Retrieve, and Delete Cookies in WebDriver?

How to Add, Retrieve, and Delete Cookies in WebDriver?Cookies are arbitrary pieces of information or message given to a Web browser by a Web server. Almost every website uses cookies in some or the other form, which are stored by the browser on the client’s computer. A cookie file stores all the information related to users and the website as a key value pair. This helps the user to remember its interaction with the website, the next time he interacts with the website on the same system.

At times it becomes essential to handle such cookies, especially while testing a website with Selenium WebDriver, as it requires you to create, update, or delete cookies as per the requirement.

In order to handle cookies in WebDriver, I have taken examples of Java code. You can create new cookies, modify existing cookies, and delete old cookies using Selenium WebDriver as shown below.

Click here to read more about Selenium WebDriver.

Note: To start with, you must import the cookies class first, by using the below-mentioned code.

import org.openqa.selenium.Cookie;

RETRIEVE 

  1. Retrieve all cookies: This method will fetch all the existing cookies

                                 public Set<Cookie> getAllCookies(){

                                return driver.manage().getCookies();}

  1. Retrieve a named cookie: This method gets a particular cookie

                               public Cookie getCookieNamed(String name){

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

  1. Retrieve a cookie’s value: This method fetches the value of a particular cookie

                              public String getValueOfCookieNamed(String name){

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

ADD

  1. Add a cookie: This method creates a new cookie

  public void addCookie(String name, String value, String domain, String path, Date expiry)               {driver.manage().addCookie(

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

  1. Add a cookie set: This method adds set of cookies for a domain

                              public void addCookiesToBrowser(Set<Cookie> cookies, String domain) { for (Cookie c : cookies) {

                              if (c != null) {

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

                               driver.manage().addCookie(

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

                               } }   }

                              driver.navigate().refresh();}

DELETE

  1. Delete a particular cookie: This method deletes a particular cookie

                            public void deleteCookieNamed(String name){

                             driver.manage().deleteCookieNamed(name);}

  1. Delete all cookies: This method deletes all cookies

                           public void deleteAllCookies() {

                           driver.manage().deleteAllCookies();}

SHARE THIS

Get A Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.