Learn About Automated Testing Using Cookies In Selenium


Cookies are data stored in a user’s computer that contains information about the user and their preference on the specific website.

For example, a cookie could save authentication information of the user session information such as browser history to improve user experiences based on their preferences. In this article, we will be learning about cookies in Selenium automated tests:

Why do we need to work with cookies in Selenium?

One great practice test automation indicates that each test should focus only on one functionality, or if we don’t want our test to execute many actions using the UI before getting to the actual condition that we want to verify.

For instance, if we are trying to test items added to the card, we can order in an E-Commerce application. But for confirming the purchase, we are required to be logged in to the application. Instead of logging in, prove that additional code, extra test execution time, and some duplication to we can use cookies with user information.

Examples

Let’s jump in and know how to use cookies in Selenium. Firstly the cookies are defined in the cookie class. Here is how the cooking class looks in Selenium C#:

The Cookie in Selenium

When we create a cookie object, it must be provided with at least a name and a value, and optionally you should also define its domain, its path, and its expiry date.

How to enable Cookies in Selenium WebDriver?

As we already mentioned, a specific situation may require us to accept cookies in the browser before performing other operations; while performing these actions manually, the cookies are set due to our interaction with the elements. But as the interactions are outside our test score, we may not want to go through the UI for setting the cookies. So you will be simply using the Cookies class in Selenium and the AddCookie() method.

  1. public void AddCookie(string cookieName, string cookieValue)
  2. {
  3.        var cookie = new Cookie(cookieName, cookieValue);
  4.        driver.Manage().Cookies.AddCookie(cookie);
  5. }

In this example, we have one for the cookie name and one for its value for stop, then we have defined a cookie variable using these attributes. In the next step, the driver.Manage().Cookies.AddCookie(cookie) line we have added to the browser.

Will see that the cookie has been added to the browser after running this code in our test.

Getting information about cookies

Many other useful cookie methods in Selenium use existing cookies for retrieving specific cookies depending on its name for retrieving all the cookies stored in the browser. A very common situation encounter is testing that the cookies were set correctly due to some of our actions in the browser. We can pass it as a parameter if we already know the cooking like this:

  1. public void GetCookieInfo(string cookieName)
  2. {
  3.        Cookie myCookie = driver.Manage().Cookies.GetCookieNamed(cookieName);           
  4. }

Then you can also use the data contained in the cookies additional in our tests. Here is an example where we can assert that the cookie value is the expected one:

Assert.IsTrue(myCookie.Value.Equals(“demoValue”));

The code will be throwing a NullReferenceException if there are no such cookies. We can also cover all the cookies in the browser, and there is no need for passing and parameters for this use the Selenium command:

driver.Manage().Cookies.AllCookies;

It will then return a collection with all the cookies stored for our page.

Cookies in Selenium

Also, read How To Use Mobile Headset Microphone With A Desktop PC?

Deleting cookies

Deleting cookies from the browser will reset our preferences. We may have tests that require your browser, so no personalized data is available in the application. We may want to remove the cookies to get better performance in the browser. Either way, we have only two options in Selenium: deleting specific cookies based on their name or deleting all cookies. Selenium commands will look like this:

  1. driver.Manage().Cookies.DeleteAllCookies();
  2. driver.Manage().Cookies.DeleteCookieNamed(“cookieName”);

We can also save a cookie as a variable and then pass it to the DeleteCookie() method. The code will look like the one below:

  1. Cookie cookie = driver.Manage().Cookies.GetCookieNamed(“cookieName”);
  2. driver.Manage().Cookies.DeleteCookie(cookie);

Why is cookie handling necessary?

While testing a web application using a Selenium Webdriver, testers could even create, update or delete a cookie. The tester has to automate different user situations such as putting an order, showing cart, payment, receipt of the confirmations of order, etc., for instance, when automating an online application.

If no cookies are saved, the user must log in before performing the above-listed test scenarios. It raises the time of coding and execution.

The solution here is to store cookies in a file. This file can then retrieve the values of each cookie and add them to your current browser session. Testers thus miss login procedures.

The way to save cookies in a file is here. This file could then retrieve the values for each cookie and then add them to the current browser session. Testers thus miss login steps in each test case since this knowledge is available at the driver session. The application server is now authenticating our browsing session and sends the tester to the desired URL directly. It is why cookie handling is required.

How to clear browser cache using Selenium?

Method #1

It is essential to clear browser cookies before we start our test. We can use the method below to delete any cookies if the Tester uses Selenium WebDriver to automate testing. Construct a void function below and call the method before browsing the program URL.

  1. public void ClearBrowserCache()
  2. {
  3. webDriver.Manage().Cookies.DeleteAllCookies(); //delete all cookies
  4. Thread.Sleep(7000); //wait 7 seconds to clear cookies.
  5. }

Method #2

  1. By executing the driver, navigate to the Selenium chrome settings tab.
  2. get(‘Chrome/ClearBrowserData’) Settings
  3. To clear the cache, click the Clear Data button.
  4. To open Chrome Developer Tools, right-click and click Inspect.
  5. Now find the item with XPath or Chropath. Use it in the Selenium script to wait until the cache is removed.

It is easy to handle Selenium cookies if we know the correct commands. By following the details of this article, testers could also easily and accurately test Selenium cookies for a good user experience in a number of the most common user scenarios.

Conclusion

Working with Selenium using existing cookies could be very helpful as we can substitute user interface steps by simply using commands, reducing test difficulty. We will adapt browser actions with built-in commands to meet our testing requirements.