Click here to learn about cookie testing. In this tutorial, we will learn –

Selenium Query Commands for cookies
Why Handle Cookies in Selenium?
Demo: Cookie handling in Selenium.
Step 1) Storing cookie information.
Step 2) Using stored cookie to login into the application.

Selenium Query Commands for cookies

In Selenium Webdriver, we can query and interact with cookies with below built-in method:

Why Handle Cookies in Selenium?

Each cookie is associated with a name, value, domain, path, expiry, and the status of whether it is secure or not. In order to validate a client, a server parses all of these values in a cookie. When Testing a web application using selenium web driver, you may need to create, update or delete a cookie. For example, when automating Online Shopping Application, you many need to automate test scenarios like place order, View Cart, Payment Information, order confirmation, etc. If cookies are not stored, you will need to perform login action every time before you execute above listed test scenarios. This will increase your coding effort and execution time. The solution is to store cookies in a File. Later, retrieve the values of cookie from this file and add to it your current browser session. As a result, you can skip the login steps in every Test Case because your driver session has this information in it. The application server now treats your browser session as authenticated and directly takes you to your requested URL.

We will use http://demo.guru99.com/test/cookie/selenium_aut.php for our demo purpose. This will be a 2 step process. Step 1) Login into application and store the authentication cookie generated. Step 2) Used the stored cookie, to again login into application without using userid and password.

Code Explanation:

Create WebDriver instance We visit the website using the driver.get(“http://demo.guru99.com/test/cookie/selenium_aut.php”) Login into the Application Read the cookie information using driver.manage().getCookies();

Store the cookie information using FileWriter Class to write streams of characters and BufferedWriter to write the text into a file to create into a file Cookies.data

“Cookies.data” file stores all cookies information along with “Name, Value, Domain, Path”. We can retrieve this information and login into the application without entering the login credentials. Once you run above code the Cookie.data file is created into the project folder structure as shown in below screen. Open the Cookie.data file, you can see login credential of the AUT is saved in the format of Cookie, see below-highlighted screen

Now, we will access the cookie generated in step 1 and use the cookie generated to authenticate our session in the application OUTPUT: You are taken directly to the login success screen without entering the input user id and password NOTE: Use hard refresh in case you see the login page after executing the above script. Conclusion Thus, you can avoid entering the username and password on the server validating them again and again for each test with the help of Selenium Webdriver, and thereby saves a lot of time.