Cookies are generally stored in a web browser to identify the user's activity when browsing a web page. Selenium WebDriver handles web browser cookies by finding, deleting, modifying, and adding the cookies. It provides an excellent mechanism to modify cookies for high-level automation testing. Let's discuss Selenium WebDriver cookies, as follows:
getCookies()
method returns cookies present in a loaded page. The following is the syntax for this function:driver.manage().getCookies();
Some of the useful tasks that you can perform with the help of getCookies()
function are discussed as follows:
Return cookies: The getCookies()
method captures all the cookies generated in a web page. Let's see a model to get to know how we can return the entire cookie ID and expiry date and time and the domain name of each and every cookie:
driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies());
getCookieNamed()
returns a specific cookie by name. The following is the syntax for this function:driver.manage().getCookieNamed(String arg0);
The following snippet returns cookies with respect to the name from the cookie file (memory of the browser); here, the generated cookie named NID
is reached from the Google page:
driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies()); System.out.println(driver.manage().getCookieNamed("NID"));
addCookie()
method injects user-defined cookies into the loading page at runtime:driver.manage().addCookie(Cookie arg0);
Adding a cookie replaces the existing cookie if one exists. Let's see how we can inject a new cookie into the current domain using the following code snippet:
driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies()); Cookie cookie = new Cookie("NID", "67=h88c3NpCuTQABjgZF2Ix8CJHivtpYDLFk5gc1_dtEnz1aP_UugPSWGukXUPeKPXOeTKZdkcWrw-DnqjsOEGhL7sURlkhamIAxsBUWH_Hh76MQ490jfT9pdwsMkWoYJAJ"); driver.manage().addCookie(cookie); System.out.println("------------------------"); driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies());
deleteCookie()
method deletes the stored cookie in the loaded webpage. The following is the syntax for this function:driver.manage().deleteCookie(Cookie arg0);
Deleting a cookie or a set of cookies means erasing certain cookies from the browser's cookie
file. The following snippet explains the deleteCookie()
method in different techniques:
driver.get("https://www.google.co.in"); Set<Cookie> cookies = driver.manage().getCookies(); for(Cookie ck :cookies) { driver.manage().deleteCookie(ck); } System.out.println(driver.manage().getCookies()); driver.get(driver.getCurrentUrl()); Cookie cookie = new Cookie("NID", "67=QKDjS3SxgW9NTe4mymVk8t0V_7314Tf1JFtkiVfb27REyOJJfW8NXzCWsandTyCYVllSK-EO7Vol2yJH1Xam4HmbKmUm7Pvm8g44dtOdm-wfW evNWKRr_UlF3Z34n28e"); driver.manage().deleteCookie(cookie);
deleteAllCookies()
method deletes all the stored cookies in a loaded web page. The following is the syntax of this function:driver.manage().deleteAllCookies();
Let's delete all the cookies generated and stored in a web page (the current domain) using the following code snippet:
driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies()); driver.manage().deleteAllCookies(); System.out.println(driver.manage().getCookies());
deleteCookieNamed()
method deletes a specific cookie by name. The following is the syntax of this function:driver.manage().deleteCookieNamed(String arg0);
This method clears the cookies with respect to the name from the cookie
file (the memory of the browser). As cookies are stored as name-value pairs, removing a cookie from the current domain is simple using Selenium WebDriver's cookie functions. Let's have a quick glance at how to remove a single cookie from the Google page using the cookie name:
driver.get("https://www.google.co.in"); System.out.println(driver.manage().getCookies()); driver.manage().deleteCookieNamed("NID"); System.out.println(driver.manage().getCookies());