Handling Cookies Selenium: In this tutorial, we are going to learn about how we can handle the web page cookies using selenium webdriver. Before start learning about cookies let us understand what is cookies and how we can store the information in cookies and how we can also retry the value from cookies. Let us start with what is HTTP cookie?
Introduction to Cookies
When you are visiting any ecommerce website and if you added something in the cart but if you have not bought that product then when we re-login that product again show in your browser. This is possible because of the HTTP cookie which is present in the browser. Let us understand what is HTTP cookie and how it works?
What is HTTP Cookie?
It is a text file which is present in the web browser of the client machine or pc which is used to store 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, that time the browser sends the stored cookies to the server and it notifies to the server about the past activities of the user.
Why Handling Cookies in Selenium Automation?
Every cookie is associated with the name, value, domain, path, expiry, and status which send to the server by which it validates that it is secure or not. also, this information helps the server to 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 so in that application there are so many features like view products, place an order, view cart and payment information like there are so many features are available. So if the cookies are not stored then when a user changes from one scenario to other scenarios every time he needs to login which is effective to the user experience.
So when user login for the first time all the information will be stored as a cookie in a file. If the user revisits the web site again then it retrieve all the information from the cookies and add those information to the current browser session so that it will not ask the login steps because all the login information are 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 of the predefined method with the help of those methods we can easily perform various operation as per our requirement. those method are below
getCookies(); This method is used to get all the cookies which are stored 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(): We can use this method to retrive a specific cookie according to its name.
manage().getCookieNamed(arg0);
addCookie(): You can use this method to 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 then you can use this method.
manage().deleteAllCookies();
Lets take an example for better understand in step by steps
- Login into the application and store the information in a cookies file
- Use the stored cookies and use those information to login into the application
Login into the application and store the information in a cookies file: In this step we are try to load the web page and enter the crential for login and check the remember me box so that t will store the login credentials and finally will click on submit button.
Use the stored cookies and use that information to login into the application
In this step, we are going to use the cookie file which we have stored the login credentials of our last browser session. In this step, we are going to retrieve all the required information and after we are going to create a cookie and add those retrieved details in that cookie with the help of addCookie() and load the same url and verify is it login successfully or not.
How to Delete Cookies Using Java Selenium With 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"); } }
Leave a Reply