Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE 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:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

Let's set up our test class:

public class SeleniumCookiesJUnitLiveTest {

    private WebDriver driver;
    private String navUrl;

    @Before
    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:

@Test
public void whenNavigate_thenCookiesExist() {
    driver.navigate().to(navUrl);
    Set<Cookie> cookies = driver.manage().getCookies();

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

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

@Test
public void whenNavigate_thenLpCookieIsHasCorrectValue() {
    driver.navigate().to(navUrl);
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

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

2.3. Cookie Properties

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:

@Test
public void whenNavigate_thenLpCookieHasCorrectProps() {
    driver.navigate().to(navUrl);
    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:

@Test
public void whenAddingCookie_thenItIsPresent() {
    driver.navigate().to(navUrl);
    Cookie cookie = new Cookie("foo", "bar");
    driver.manage().addCookie(cookie);
    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:

@Test
public void whenDeletingCookie_thenItIsAbsent() {
    driver.navigate().to(navUrl);
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");

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

    driver.manage().deleteCookie(lpCookie);
    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:

@Test
public void whenOverridingCookie_thenItIsUpdated() {
    driver.navigate().to(navUrl);
    Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
    driver.manage().deleteCookie(lpCookie);

    Cookie newLpCookie = new Cookie("lp_120073", "foo");
    driver.manage().addCookie(newLpCookie);

    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

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!