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.

Examples

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.

 

add 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 AddCookie() method.

public void AddCookie(string cookieName, string cookieValue)

{

var cookie = new Cookie(cookieName, cookieValue);

driver.Manage().Cookies.AddCookie(cookie);

}

 

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 Cookie variable and gave it these properties. On to the drive.Manage() I merely added the cookie to the browser using the AddCookie(cookie) line.

 

Getting cookies 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:

public void GetCookieInfo(string cookieName)

{

Cookie myCookie = 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(myCookie.Value.Equals(“demoValue”));

A NullReferenceException will be thrown by the function 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 that the program doesn’t have access to any 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(“cookieName”);

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 cookie = driver.Manage().Cookies.GetCookieNamed(“cookieName”);
driver.Manage().Cookies.DeleteCookie(cookie);

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.