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.
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.
- public void AddCookie(string cookieName, string cookieValue)
- var cookie = new Cookie(cookieName, cookieValue);
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:
- public void GetCookieInfo(string cookieName)
- Cookie myCookie = driver.Manage().Cookies.GetCookieNamed(cookieName);
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:
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:
It will then return a collection with all the cookies stored for our page.
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:
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:
- Cookie cookie = driver.Manage().Cookies.GetCookieNamed(“cookieName”);
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?
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.
- public void ClearBrowserCache()
- webDriver.Manage().Cookies.DeleteAllCookies(); //delete all cookies
- Thread.Sleep(7000); //wait 7 seconds to clear cookies.
- By executing the driver, navigate to the Selenium chrome settings tab.
- get(‘Chrome/ClearBrowserData’) Settings
- To clear the cache, click the Clear Data button.
- To open Chrome Developer Tools, right-click and click Inspect.
- 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.
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.