Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

In this article, we'll have a quick look at how to use cookies with Selenium WebDriver in Java.

We'll talk a bit about some use cases, and then we'll jump straight into code.

2. Working with Cookies

An everyday use case for manipulating cookies is to persist our session between tests.

An even simpler scenario is when we want to test that our backend is setting cookies properly.

In the next sections, we'll briefly talk about handling cookies while providing simple code examples.

2.1. Setup

We'll need to add the selenium-java dependency to our project:


Next, we should download the latest version of the Gecko driver.

Now let's set up our test class:

public class SeleniumCookiesJUnitLiveTest {

    private WebDriver driver;
    private String navUrl;

    public void setUp() {
        Capabilities capabilities = DesiredCapabilities.firefox();
        driver = new FirefoxDriver(capabilities);
        navUrl = "https://baeldung.com";

2.2. Reading Cookies

Next, we'll implement a simple test to verify that cookies exist in our driver after we've navigated to a webpage:

public void whenNavigate_thenCookiesExist() {
    Set<Cookie> cookies = driver.manage().getCookies();

    assertThat(cookies, is(not(empty())));

Often, we might want to search for a specific cookie:

public void whenNavigate_thenLpCookieIsHasCorrectValue() {
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

    assertThat(lpCookie.getValue(), containsString("www.baeldung.com"));

A cookie can be associated with a domain, have an expiry date, and much more.

Let's take a look at some common cookie properties:

public void whenNavigate_thenLpCookieHasCorrectProps() {
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

    assertThat(lpCookie.getDomain(), equalTo(".baeldung.com"));
    assertThat(lpCookie.getPath(), equalTo("/"));
    assertThat(lpCookie.getExpiry(), is(not(nullValue())));
    assertThat(lpCookie.isSecure(), equalTo(false));
    assertThat(lpCookie.isHttpOnly(), equalTo(false));

2.4. Adding Cookies

Adding a cookie is a straightforward process.

We create the cookie and add it to the driver using the addCookie method:

public void whenAddingCookie_thenItIsPresent() {
    Cookie cookie = new Cookie("foo", "bar");
    Cookie driverCookie = driver.manage().getCookieNamed("foo");

    assertThat(driverCookie.getValue(), equalTo("bar"));

2.5. Deleting Cookies

As we might've expected, we can also delete a cookie using the deleteCookie method:

public void whenDeletingCookie_thenItIsAbsent() {
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

    assertThat(lpCookie, is(not(nullValue())));

    Cookie deletedCookie = driver.manage().getCookieNamed("lp_120073");

    assertThat(deletedCookie, is(nullValue()));

2.6. Overriding Cookies

Although there's no explicit method for overriding a cookie, there's a simple way.

We can delete the cookie and add a new one with the same name but a different value:

public void whenOverridingCookie_thenItIsUpdated() {
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

    Cookie newLpCookie = new Cookie("lp_120073", "foo");

    Cookie overriddenCookie = driver.manage().getCookieNamed("lp_120073");

    assertThat(overriddenCookie.getValue(), equalTo("foo"));

3. Conclusion

In this quick tutorial, we learned how to work with cookies using Selenium WebDriver in Java through quick and practical examples.

As always, the code is available over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Comments are closed on this article!