Managing Cookies with Selenium for Java
Managing Cookies with Selenium for Java
Learn how to read, add, edit, and delete cookies using Selenium for Java
In today’s recipe, I’ll show you how to read, add, edit, and delete cookies in Selenium WebDriver.
Within dev tools, if you navigate to the Applications tab and look at the Cookies section under Storage, you’ll see the cookies for your website. In my case, I have 2 cookies here — vegetable, which has a value of broccoli, and protein, which has a value of chicken.
Let’s jump right into it and manage these cookies via Selenium WebDriver.
How to Read a Cookie with Selenium
In our first exercise, let’s just read a cookie.
We’re going to say driver.manage().getCookieNamed() and we can specify the name of a cookie. So, we know we already have 2 by default, let’s just give one of them, the “protein” one.
This will return a Cookie object and this Cookie object is actually part of WebDriver.
We’ll assign this to a variable that we’ll call “cookie”. Now we can do an assertion. So we can say assertEquals(). Let’s say our expected value is “chicken”, and we can get the actual value by saying cookie.getValue().
Great. Let’s run this and see how it goes.
Excellent. That one worked.
How to Add a Cookie with Selenium
Okay, now we can add a cookie.
So inside of here, we’ll go ahead and create a new Cookie object.
We’ll call it “cookie”. For this one, we’re going to call this Cookie(“fruit”), and its value, let’s say, “apple”. Great.
Now we can say driver.manage(), and there’s a method here called addCookie().
So, we can specify that and pass in the “cookie” that we’ve defined. This will add this brand-new cookie to our website.
Just to make sure that the cookie is there, let’s do an assertEquals() and we’ll say that the “cookie” that we defined. Let’s go ahead and get its value by calling cookie.getValue().
Then we’ll say driver.manage(), and we will ask for a cookie with the same name as the one that we created. So we’ll say getCookieNamed(cookie.getName()).
This will give us a Cookie object, so we also need to go ahead and getValue() of that. Therefore, we’re saying the cookie that we’ve defined here; we expect its value to be the same as whatever you get from the browser. Okay, let’s run this one.
Awesome. That one worked as well. Perfect.
How to Edit Cookies with Selenium
Moving right along, let’s go ahead and edit a cookie.
Let’s create the fruit cookie just in case it doesn’t already exist. We’ll call this one, “originalCookie” and we’ll specify its name as “fruit” and its value as “apple”.
Okay, now let’s go ahead and tell WebDriver to go ahead and addCookie(originalCookie) for us. Perfect.
Now what we’re going to do is create another Cookie object in our code. We’ll call this one, “editedCookie”. This one will have the same name as the “originalcookie”, but we’re going to change the value to, let’s say, “mango”. Great.
Now there’s no direct way to just say, edit a cookie.
Let me show you. If we say driver.manage(), there’s addCookie, deleteCookie, getCookie, and then you can do getAllCookies or deleteAllCookies. But there’s not an edit.
What you do is you have to call, addCookie() again and inside of here, we’ll specify our “editedCookie”. Now notice, this has the same name, “fruit”, but it has a different value. So, that should override that.
Just to make sure that it does, let’s go ahead and do an assertEquals(), and we’ll say that we’re expecting the editedCookies.getValue to equal the new value.
We’ll say driver.manage() and we’ll say .getCookieNamed() and the cookie name for the original cookie or edited cookie, they’re both the same, but I’m just going to say originalCookie.getName(). So, I want that cookie, and then I want to .getValue.
What we are saying is that the original cookie’s name should have the edited cookie’s value. Now let’s run this one and make sure it works.
Excellent. Okay.
How to Delete Cookies with Selenium
Now let’s delete a cookie.
We’ll go ahead and create the new Cookie object. And we’ll say, this time it’s “fruit” and the value is “mango”. I hate mango. So, I’m happy to delete this cookie.
First, we’re going to go ahead and add it just in case it doesn’t already exist. So, we’re saying driver.manage().addCookie(cookie), and we pass in this cookie.
Now we’re going to delete the cookie — it’s another manage command, driver.manage() but this time we deleteCookie().
You can either say deleteCookie(nameofcookie) and pass it in, or you can say deleteCookieNamed(nameofcookie). Okay, I’ll just go ahead and pass in the whole cookie. Excellent.
Now let’s just verify that it’s not there. I’m going to assertNull() this time, and I’ll say driver.manage().getCookieNamed(cookie.getName()) and let’s pass in our cookie name.
All right, and that should be null. Let’s run this one and test it out.
And perfect. That passes well. Excellent.
Summing It Up – Testing Cookies with Selenium Java
Let’s do a little quick recap.
To access a cookie and read it and its value, we can just do driver.manage()getCookieNamed(). You pass in the name, and you’ll get a Cookie object back. From there, you can do a cookie.getValue() and verify its value.
To add a cookie, again we can create a new Cookie object. We say driver.manage(). This time we call addCookie() and pass it in.
To edit a cookie, there is no edit cookie method. Instead, you must add the cookie again, but change whatever you want to change about it. So, if there’s a cookie that already exists with a certain name, you need to pass that name in again, and then just change the value and call addCookie().
Fnally, to delete a cookie, you can just call deleteCookie() and specify either the Cookie object, or you can say deleteCookieNamed() and give it just the text value.
All right, great job. That’s how you manage cookies.
Resources
- Git Repo – CookieTests.java
- Applitools Kitchen – Testing Cookies
Schedule a free Applitools Demonstration
Request DemoHere’s the code sample from our tutorial on how to read, add, edit, and delete cookies using Selenium for Java