All IT Courses 50% Off
Selenium Tutorials

Working with Cookies in Selenium Automated Tests

Small data files known as cookies, or HTTP cookies, are stored on a user’s computer and include information about the user and their preferences on a certain website Selenium Automated Tests. Check our Selenium automation testing course to learn more.

Why is handling cookies necessary in automating testing?

Cookies can be used by websites for a variety of things, such as saving visitor session data.

Automated test execution may encounter a potential issue. It’s possible that the test website stores particular data in the same cookie for several tests.

Let’s take the scenario where you are adding an item to a shopping cart to test it. Data may be saved in a cookie if one test adds the item to the shopping cart.

All IT Courses 50% Off

The theory behind the second test can presuppose that the trolley is empty.

However, because the cookie from the first test is still kept, the second test fails.

It’s crucial to make sure that all of your tests begin completely blank, with no previous test results.

Anatomy of a cookie

The following details can be found in a cookie:

  • Name: The cookie’s name is contained in this flag.
  • Value: The cookie’s value is contained in this flag.
  • Domain: This identifies the domain on which the cookie is located.
  • Path: The required URL is contained in this flag’s requested URL.
  • Expires/ Max-Age: The cookie’s expiration date or maximum age is indicated by the Expires or Max-Age field.
  • HTTPOnly: Should just HTTP be used to access this cookie?
  • Secure: This establishes whether the cookie can only be transmitted via HTTPS.
  • SameSite: If the cookie is utilising the experimental SameSite feature, this flag provides the values (strict or lax).

Let’s begin by discussing how to use cookies in Selenium. First off, cookies are defined in the Cookie class. As a result, we must specify a cookie object’s domain, path, and expiration date in addition to giving it at least a name and a value.

Adding Cookies

When we perform these operations manually, we interact with the web elements, which results in the creation of cookies. Because these interactions fall beyond the purview of our test, we don’t want to use the UI to set the cookies. So, we’ll just use the AddCookie() method and the Cookies class from Selenium.

Working with Cookies in Selenium Automated Tests

As an illustration, take for example that I created a generic method that takes two string parameters: one for the cookie name and one for its value. Afterwards, I made a newCookie variable and gave it these characteristics. Continuing the drive.Manage() I basically used the AddCookie(newCookie) line to add the cookie to the browser.

Getting the Cookie Information

We may access either every cookie saved in the browser or just one cookie by using other useful Cookie methods in Selenium, depending on its name.

An example case we might encounter is testing whether the cookies were correctly set as a result of some of our browser operations. The cookie name can be supplied as a parameter as shown below if it is already known:

Cookie getCookieInformation = driver.Manage().Cookies.GetCookieNamed(cookieName);

We shall then use the information contained in the cookies in our further tests. In this instance, we may state that the cookie value is as expected:


In the event that such a cookie cannot be located, the function will throw a NullReferenceException.

It is also possible to get all of the browser’s cookies. The Selenium command is all that is necessary; no other parameters are needed.


By doing this, a compilation of all cookies that have been saved for our page will be delivered.

Deletion of cookies

If the cookies are removed from the browser, the preferences will be reset. For testing, you might need a clean browser to prevent the software from accessing sensitive data. Alternatively, you may want to remove the cookies to make your browser run faster. You have two options in Selenium: either delete all cookies or delete specific cookies based on their names.

Working with Cookies in Selenium Automated Tests

As an alternative, a cookie can be saved as a variable and then supplied to the DeleteCookie() method.

How to use the Selenium Cookie API with a Selenium Automated Tests?

You can use Remote Webdriver and the Selenium Cookie methods on the Remote WebDriver object to use the Selenium Cookie API with a Selenium Grid.

You might prefer to run tests on a (Cloud) Selenium grid rather than on your personal machine while conducting Selenium tests. This has several benefits, including:

  • Increased scalability: use a Cloud grid-like TestingBot to execute tests concurrently.
  • Increased reliability:  connectivity or OS faults can be recognized immediately, increasing test reliability.
  • Performance: You can use computers that are more powerful than your own to do the browser tests.

How can I clear the browser cache automatically?

You might want to delete your browser’s cache, which includes cookies, history state, and other saved data, in between automated tests.

It’s critical that tests can begin in a pristine state.

The test’s assertion may not succeed if there are artefacts left over from earlier test sessions, such as an item still in a shopping cart.

We’ll show two ways to automatically erase the browser’s cache:

1.DeleteAllCookies with Selenium WebDriver

You can erase all cookies using the Selenium Cookie API provided by Selenium WebDriver. This method is called: 


2.Clear Browser Data with Chrome driver

To clean the browser cache, utilise Chromedriver. This only functions when testing on Chrome, of course.

We advise using the Selenium Cookie API if you are testing on a different browser.


Working with cookies in Selenium may be quite advantageous as it enables us to streamline the tests by swapping out UI chores with simple commands. Check out the Selenium training online to learn more about Cookies in Selenium.

Facebook Comments

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.

Related Articles

Back to top button