Cookie are information stored on computers.It contains name and value of the cookie along with name and value it stores other information like domain name, expiry date, path, secure and http only flags.
We are going to cover below topics in this article
- Use of get, add and delete operations in selenium
- Create new cookie using Cookie class.
Use of get, add and delete operations in selenium
Selenium allows to read/get, add and delete operation on cookies (ref WebDriver.Options ).
To Read/Get
- getCookies() → return set of cookies (Set<Cookie>)
- getNamedCookie(cookieName) → return cookie by name.
Java code for get operation:
Set<Cookie> cookies=driver.manage().getCookies();
Cookie cookie=driver.manage().getNamedCookie(cookieName);
To Add
- addCookie(cookie) → add cookie to current active session.
Java code for add cookie:
driver.manage().addCookie(cookie);
To Delete
- deleteAllCookies() → delete all cookies for current domain.
- deleteCookie(cookie) → delete specific cookie.
- deleteCookieNamed(cookieName) → delete cookie by name.
Java code to delete cookie(s):
driver.manage().deleteAllCookies();
driver.manage().deleteCookie(cookie);
driver.manage().deleteCookieNamed(cookieName);
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.Cookie; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "./chromedriver.exe"); WebDriver driver; ChromeOptions cOptions = new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver = new ChromeDriver(cOptions); driver.get("https://www.societyhive.com/"); Cookie oldCookie = null; // Get cookies System.out.println("\n Get all cookies."); Set<Cookie> cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie); } System.out.println("\n Get cookie by name."); oldCookie=driver.manage().getCookieNamed("__RequestVerificationToken"); System.out.println(oldCookie); // Delete cookies System.out.println("\n Delete cookie by name _gid"); driver.manage().deleteCookieNamed("_gid"); cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie); } System.out.println("\n Delete __RequestVerificationToken cookie"); for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase("__RequestVerificationToken")) { driver.manage().deleteCookie(cookie); } } cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie); } System.out.println("\n Delete all cookies"); driver.manage().deleteAllCookies(); cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie); } // Add cookie System.out.println("\n Add old cookie __RequestVerificationToken"); driver.manage().addCookie(oldCookie); cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie); } driver.quit(); } } |
Create new cookie and add to webdriver
1 2 |
Cookie myCookie= new Cookie("nameOfCookie", "ValueOfCookie"); driver.manage().addCookie(myCookie); |
Different parameterized constructors can be used to create cookie in selenium where name and value are mandatory.
- Cookie(String name, String value)
- Cookie(String name, String value, String path)
- Cookie(String name, String value, String path,Date expiry)
- Cookie(String name, String value, String domain, String path,Date expiry)
- Cookie(String name, String value, String domain, String path,Date expiry, boolean isSecure)
- Cookie(String name, String value, String domain, String path,Date expiry, boolean isSecure, boolean isHttpOnly)