Cookies, or HTTP cookies, are pieces of data stored on the user’s computer containing information about the user and the user’s preference on a specific website. For example, a cookie can store the authentication information of the user or session information such as browsing history, so the website can improve the user’s experience based on their preferences.
In this article, we’ll see the commands used for working with cookies in Selenium.
Why we need to work with cookies in Selenium 🍪
One of the good practices in test automation states that each test should focus on only one functionality. This means that we don’t want our tests to perform too many actions through the UI before we get to the actual condition we want to verify. Let’s look at an example: you’re trying to test items added to the cart that can be ordered in an e-commerce application. But to finalize the purchase, you need to be logged in to the application. Instead of logging in through the UI, which means additional lines of code, extra test execution time, and probably some test duplication, too, you can use cookies with the user information.
Examples
Let’s dive in and go through how to use cookies in Selenium.
First off, the cookies are defined in the Cookie class. Here’s how the class looks in Selenium C#:
This means that when we create a cookie object, we must give it at least a name and a value, and optionally also define its domain, its path, and its expiry date.
Adding cookies
As I already mentioned, a common scenario may require us to already have some cookies set up in the browser, before we can perform other operations. When we perform these actions manually, the cookies are set as a result of our interaction with the web elements. But since these interactions are outside our test scope, we don’t want to go through the UI to set the cookies. So we’ll simply use the Cookies class in Selenium, and the AddCookie() method.
public void AddCookie(string cookieName, string cookieValue) { var cookie = new Cookie(cookieName, cookieValue); driver.Manage().Cookies.AddCookie(cookie); }
In this example, I created a generic method that takes two string parameters – one for the cookie name, and one for its value. Then I defined a Cookie variable with these attributes. Next, on the driver.Manage().Cookies.AddCookie(cookie) line, I simply added the cookie to the browser.
After running this code in our test, we will see the cookie added in the browser – see below my results on the https://example.testproject.io/web/ web page:
Getting cookies information
Other useful Cookies methods in Selenium allow us to retrieve a specific cookie, based on its name, or to retrieve all the cookies stored in the browser.
A common scenario we might encounter is to test that the cookies were correctly set as a result of some of our actions in the browser. If we already know the cookie name, we can simply pass it as a parameter, like this:
public void GetCookieInfo(string cookieName) { Cookie myCookie = driver.Manage().Cookies.GetCookieNamed(cookieName); }
Then, we can use the information contained in the cookies further in our tests. Here’s an example where we can assert that the cookie value is the expected one:
Assert.IsTrue(myCookie.Value.Equals("demoValue"));
If no such cookie is found, the code will throw a NullReferenceException.
We can also retrieve all the cookies in the browser. We don’t need to pass any parameters for this, just use the Selenium command:
driver.Manage().Cookies.AllCookies;
This will return a collection with all the cookies stored for our page.
Deleting cookies
Deleting the cookies from the browser will reset the preferences. You might have tests that need a clear browser, so no personalized data is available in the application. Or you might want to remove the cookies to get better performance in the browser. Either way, you have two options in Selenium: delete specific cookies (based on their name), or delete all cookies.
The Selenium commands look like this:
driver.Manage().Cookies.DeleteAllCookies(); driver.Manage().Cookies.DeleteCookieNamed("cookieName");
Alternatively, you can save a cookie as a variable, then pass it to the DeleteCookie() method. The code will look as below:
Cookie cookie = driver.Manage().Cookies.GetCookieNamed("cookieName"); driver.Manage().Cookies.DeleteCookie(cookie);
Conclusions
Working with cookies in Selenium can be quite helpful because we can replace UI steps with simple commands, therefore reducing the complexity of the tests. Using the built-in commands, we can customize the browser behavior to fit our testing purposes.
Thanks for this blog. It helps me a lot to learn how cookies work in selenium testing. I have learned selenium testing from Gayatri Mishra’s website which is really good site for the beginners to learn selenium testing. That website really helped me a lot to learn Selenium.