Handling Cookies Selenium: In this tutorial, we will learn how to handle web page cookies using Selenium webdriver. Before learning about cookies, let us understand what cookies are, how we can store the information in cookies, and how we can also retry the value from cookies. Let us start with what an HTTP cookie is.
Introduction to Cookies
When you are visiting any ecommerce website and if you added something to the cart but have not bought that product, then when you re-login, that product again shows in your browser. This is possible because of the HTTP cookie which is present in the browser. Let us understand what an HTTP cookie is and how it works.
What is an HTTP Cookie?
It is a text file in the web browser of the client machine or PC, which stores the information about the user and their preferences as a key-value pair when a user does the browsing. When a user revisits a website again, the browser sends the stored cookies to the server and notifies the server about the user’s past activities.
Why Handling Cookies in Selenium Automation?
Every cookie is associated with the name, value, domain, path, expiry, and status sent to the server by which it validates whether it is secure or not. also, this information helps the server validate the client using the cookies. But when we are testing a web application at that time, we may need to update, create, or delete the existing cookies.
Suppose you are testing an E-commerce website in that application; there are so many features like viewing products, placing an order, viewing a cart, and payment information like there are so many features available. So if the cookies are not stored, a user changes from one scenario to another every time he needs to log in, which is effective for the user experience.
So when the user logs in for the first time, all the information will be stored as a cookie in a file. If the user revisits the website, then it retrieves all the information from the cookies and adds that information to the current browser session so that it will not ask for the login steps because all the login information is available so that the server authenticated with that information and directly takes you to the requested page instead of the login page.
Query for Handle Cookies Using Selenium
To deal with the cookies, selenium provides some predefined methods; with the help of those methods, we can easily perform various operations per our requirements. those methods are below
getCookies(): This method stores all the cookies in the web browser.
manage().getCookies();
//To fetch a Cookie by name System.out.println(driver.manage().getCookieNamed(cookieName).getValue()); //To return all the cookies of the current domain Set<Cookie> cookiesForCurrentURL = driver.manage().getCookies(); for (Cookie cookie : cookiesForCurrentURL) { System.out.println(" Cookie Name - " + cookie.getName() + " Cookie Value - " + cookie.getValue())); }
getCookieNamed(): This method can retrieve a specific cookie according to its name.
manage().getCookieNamed(arg0);
addCookie(): This method can create and add the cookie in the cookie file.
manage().addCookie(arg0)
Cookie cookie = new Cookie(“cookieName”, “cookieValue”);
driver.manage().addCookie(cookie);
deleteCookie(): When we want to delete all the cookies, you can use this method.
manage().deleteAllCookies();
Let’s take an example for better understanding in step-by-step steps
- Login into the application and store the information in a cookies file
- Use the stored cookies and that information to log into the application.
Login into the application and store the information in a cookies file: In this step, we are trying to load the web page, enter the credentials for login, check the Remember me box so that it will store the login credentials, and finally click on the submit button.
Use the stored cookies and that information to log into the application.
In this step, we will use the cookie file in which we have stored the login credentials of our last browser session. In this step, we will retrieve all the required information, then create a cookie and add those retrieved details in that cookie with the help of addCookie() load the same URL, and verify whether the login is successful.
How do you delete cookies using Java Selenium with the program?
package com.selenium.mix; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class deletecookies { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); driver= new FirefoxDriver(); String URL="http://www.flipcart.com"; driver.navigate().to(URL); driver.manage().deleteAllCookies(); System.out.println("Cookies Deleted"); } }