Skip to main content

Technical

Working with Cookies in Selenium Automated Tests

Man on mounted desktop computer working.

We’ll look at the Selenium commands in this blog that are used to work with cookies. Cookies, or HTTP cookies, are small data files that are saved on a user’s computer that include details about the user and their preferences on a particular website.

Let’s get started and go over using cookies in Selenium. First off, the Cookie class has a definition of cookies. This means that we must define a cookie object’s domain, path, and expiration date in addition to giving it at least a name and a value.

 

Adding Cookies

The cookies are created as a result of our interactions with the web elements when we carry out these tasks manually. We don’t want to use the UI to set the cookies because these interactions are outside the scope of our test. Therefore, we’ll just utilize Selenium’s Cookies class and the AddCookie() method.

public void AddingACookie(string nameOfCookie, string valueOfCookie)
{
var newCookie = new Cookie(nameOfCookie, valueOfCookie);
driver.Manage().Cookies.AddCookie(newCookie);
}

For the purposes of this illustration, I developed a generic method that accepts two string parameters: one for the cookie name and one for its value. I then created a newCookie variable and gave it these properties. On to the drive.Manage() I merely added the cookie to the browser using the AddCookie(newCookie) line.

 

Getting the Cookie Information

Other helpful Cookies methods in Selenium enable us to either retrieve all of the cookies saved in the browser or a single cookie, depending on its name.

Testing whether the cookies were properly set as a result of some of our browser operations is a typical scenario we might run into. If the cookie name is already known, it can be passed as a parameter as follows:

Cookie getCookieInformation = driver.Manage().Cookies.GetCookieNamed(cookieName);

The data in the cookies will then be used in our testing going forward. Here is an instance when we can claim that the cookie value is what was anticipated:

Assert.IsTrue(getCookieInformation.Value.Equals(“cookieValue”));

The function will throw a NullReferenceException if such a cookie cannot be found.

All of the browser’s cookies can also be retrieved. We only need to use the Selenium command; no parameters are required.

driver.Manage().Cookies.AllCookies;

This will deliver a collection containing every cookie that has been saved for our page.

Deletion of cookies

The preferences will be reset if the cookies are deleted from the browser. You might require a clear browser for tests, so the program doesn’t have access to personal information. Or you might wish to delete the cookies to improve browser performance. In Selenium, you have two choices: erase all cookies or delete individual cookies based on their names.

The Selenium commands appear as follows:

driver.Manage().Cookies.DeleteAllCookies();
driver.Manage().Cookies.DeleteCookieNamed("nameOfCookie");

A cookie can be saved as a variable and then passed to the DeleteCookie() method as an alternative. This is how the code will appear:

Cookie cookieObject = driver.Manage().Cookies.GetCookieNamed("nameOfCookie");
driver.Manage().Cookies.DeleteCookie(cookieObject);

Conclusions

Working with cookies in Selenium may be very beneficial because it allows us to simplify the tests by substituting straightforward commands for UI tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mangesh Sonwane

Mangesh Sonwane is an Associate Technical Consultant at Perficient in Nagpur GDC. He has an experience of 1+ years in Drupal and AEM. Apart from this passionate about learning new changes and expanding his knowledge in Automation. He is very committed to his work and is always ready to face any challenging projects.

More from this Author

Follow Us