Selenium-10: Cookies


In this post, we will learn what a cookie is and all details of Selenium Webdriver’s cookies API. Selenium webdriver can handle cookies with its built-in methods. We can add, delete, and query cookies.


This post is designed for SW test professionals who have some information about selenium web automation. At the end of this tutorial, you will know how to work with cookies in selenium webdriver.


Nice to have: Before starting to read this post it is better to read previous selenium tutorials.

What is a cookie?

Cookies comprise of information about us and our preferences. A website places this information into our computer so that it can remember it in the future. Simply, a website can recognize you with cookies and look up your preferences. For instance, when we select language preference of a website, then the browser will save that information into a little document (cookie) on our PC. Next time we visit that website, browser will read that cookie then remember our previous language preference, and changes website’s language automatically. As remembering your language preference, cookies can contain and remember any kind of information such as your visit time of a website, products that you added in your shopping basket, all the links that you clicked on a website, etc.

Cookie mechanism allows that the server (webpage) holds its own information about a user on the user’s PC. We can view the details of any cookie and also can delete them. Web users have to agree to let cookies be saved for them into their PC to websites can remember their cookies for better service. Also, in order to reduce cookie size, a unique ID is stored into that cookie and the rest of the personal data is stored in website’s servers. This kind of cookies are called third-party cookies. By the way, one of the most important cookies are the authentication cookies that provides login and account status to the web servers. The security of these cookies generally depends on the security of the related website and user’s web browser, also encryption of cookie’s data.

For more information of cookies please go below links:

Selenium Webdriver Cookies API

In selenium webdriver we can query and interact with cookies with below built-in methods.

Interact and Query with Cookies


  • .getCookies()returns the list of all cookies
  • .getCookieNamed(“cookie name”)returns specific cookie according to its name
  • .addCookie(“cookie”)creates and adds a cookie
  • .deleteCookie(“cookie”)deletes specific cookie
  • .deleteCookieName(“cookie name”)deletes specific cookie according to its name
  • .deleteAllCookiesdelete all the cookies


We can create cookies with Cookie.Builder in an easy and convenient way instead of using a constructor to create a new cookie. You can build a new cookie by typing “new Cookie.Builder”. Its constructor gets some parameters such as name, value, path, domain, expiration date, is secure, etc. and at the last we can finish the declaration with .build() to create a cookie with the builder. Name and value are mandatory fields for creating a cookie with Cookie.Builder.


  1. Go to
  2. Get all the Cookies and print the total number of Cookies.
  3. Click “I want mik with my cookie!”
  4. Get the cookie which name is “cookie” with getName() method and print its value.
  5. Assert that “cookie” value is “Chocolate_Chip”
  6. Copy above “cookie” as copiedCookie and print it and total cookie number.
  7. Create new cookie as “buildedCookie” with Builder and print it.
  8. Delete the “cookie”
  9. Print total cookie number after deleting “cookie”.
  10. Add new cookie by using “buildedCookie”
  11. Add new cookie by usingcopiedCookie”
  12. Print total cookie number after adding two cookies.
  13. Delete buildedCookie by using .deleteCookieName
  14. Delete copiedCookie by using .deleteCookie
  15. Print total cookie number after deleting two cookies.
  16. Delete all cookies.
  17. Print total cookie number after deleting all the cookies.
  18. Refresh page then select Gingerbread and click “I want milk with my cookie!” and click “Get a Cookie!” button.
  19. Get the cookie which name is “cookie” and assert that its value is “Ginderbread_milk” as shown below.


Test Code:

Console Output:


Example 2:

If you want to manually create a cookie and add, you can use below method. In below example our aim is to create a new cookie and set next month to its expiration date.




Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary 2- Download latest Jmeter version 3.0 or higher. 3- Download Jmeter PluginsManager JAR file and...
By | 2017-01-13T21:33:46+00:00 January 17th, 2016|Selenium, Test Automation, Web Automation|0 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

Leave A Comment