Site icon

Handle Cookies using Selenium WebDriver


        @Test
	public void VerifyAddedCookie() {
	    driver.get("https://makeinjava.com");
    
         // We are passing parameters, name = CookieName and value=12345678
	    Cookie cookie = new Cookie("CookieName", "12345678");
	    driver.manage().addCookie(cookie);
	    Cookie newAddedCookie = driver.manage().getCookieNamed("CookieName");
	 
          // Verify the cookie value
	    assertEquals(newAddedCookie.getValue(), "12345678");
	}
  1. driver.manage().deleteCookie(Cookie cookie);  --  delete a specific cookie
  2. driver.manage().deleteCookieNamed(String name); --  delete named cookie from the current domain
  3. driver.manage().deleteAllCookies();  --  delete all the cookies for the current domain

The method getCookies() returns Set of cookies for the current domain.


Set<Cookie> cookies = driver.manage().getCookies();

The method getCookieNamed(String name) returns cookie with a given name, or null if no cookie found with the given name.


driver.manage().getCookieNamed(String name);

Exit mobile version