cookie: An HTTP cookie (also called web cookie, Internet cookie, browser cookie or simply cookie, the latter which is not to be confused with the literal definition), is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website.
We can perform required task with respect to browser cookies in Selenium Webdriver and can add , delete, delete particular cookie by passing the name.
Add Cookie
Method Name: addCookie(Cookie cookie)
Syntax: driver.manage().addCookie(arg0);
Description: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Example:
Cookie name = new Cookie("firefoxcookie", "123456789123");
driver.manage().addCookie(name);
Get the Cookie with Specific Name
Method Name: getCookieNamed(java.lang.String name)
Syntax: driver.manage().getCookieNamed(arg0);
Description: To Get a cookie with a given name.
Example:
driver.manage().getCookieNamed("firefoxcookie");
Get Cookies
Method Name: getCookies()
Syntax: driver.manage().getCookies();
Description: Get all the cookies for the current domain.
Example:
Set<Cookie> cookiesList1 = driver.manage().getCookies();
for (Cookie getcookies : cookiesList1) {
System.out.println(getcookies);
}
Delete Cookie with Name
Method Name: deleteCookieNamed(java.lang.String name)
Syntax: driver.manage().deleteCookieNamed(arg0);
Description: Delete the named cookie from the current domain.
Example:
driver.manage().deleteCookieNamed("firefoxcookie");
Delete All Cookies
Method Name: deleteAllCookies()
Syntax: driver.manage().deleteAllCookies();
Description: It will delete all the cookies for the current domain.
Example:
driver.manage().deleteAllCookies();
We can perform required task with respect to browser cookies in Selenium Webdriver and can add , delete, delete particular cookie by passing the name.
Add Cookie
Method Name: addCookie(Cookie cookie)
Syntax: driver.manage().addCookie(arg0);
Description: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Example:
Cookie name = new Cookie("firefoxcookie", "123456789123");
driver.manage().addCookie(name);
Get the Cookie with Specific Name
Method Name: getCookieNamed(java.lang.String name)
Syntax: driver.manage().getCookieNamed(arg0);
Description: To Get a cookie with a given name.
Example:
driver.manage().getCookieNamed("firefoxcookie");
Get Cookies
Method Name: getCookies()
Syntax: driver.manage().getCookies();
Description: Get all the cookies for the current domain.
Example:
Set<Cookie> cookiesList1 = driver.manage().getCookies();
for (Cookie getcookies : cookiesList1) {
System.out.println(getcookies);
}
Delete Cookie with Name
Method Name: deleteCookieNamed(java.lang.String name)
Syntax: driver.manage().deleteCookieNamed(arg0);
Description: Delete the named cookie from the current domain.
Example:
driver.manage().deleteCookieNamed("firefoxcookie");
Delete All Cookies
Method Name: deleteAllCookies()
Syntax: driver.manage().deleteAllCookies();
Description: It will delete all the cookies for the current domain.
Example:
driver.manage().deleteAllCookies();
Full Example
import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Handlecookie { public static void main(String args[]) { // Initialize Firefox Webdriver WebDriver driver = new FirefoxDriver(); //Maximize browser window driver.manage().window().maximize(); //Go to desire wbesite driver.get("http://www.google.com"); //Set timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //pass name and value for cookie as parameters Cookie name = new Cookie("firefoxcookie", "123456789123"); // Add Cookie driver.manage().addCookie(name); // Get the Cookie with Specific Name System.out.println(driver.manage().getCookieNamed("firefoxcookie")); System.out.println("======================================="); //Get all Cookies Set<Cookie> cookiesList1 = driver.manage().getCookies(); for (Cookie getcookies : cookiesList1) { System.out.println(getcookies); } System.out.println("======================================="); // Delete Cookie with Name driver.manage().deleteCookieNamed("firefoxcookie"); System.out.println("======================================="); //Check deleted or not firefoxcookie Cookie Set<Cookie> cookiesList2 = driver.manage().getCookies(); for (Cookie getcookies : cookiesList2) { if (getcookies.getName().equals("firefoxcookie")) { System.out.println(driver.manage().getCookieNamed("firefoxcookie")); } else { System.out.println("There is no cookies name firefoxcookie"); break; } } System.out.println("======================================="); //Delete all the cookies of the domain driver.manage().deleteAllCookies(); // Check all the cookies deleted or not Set<Cookie> cookiesList3 = driver.manage().getCookies(); if (cookiesList3.size() == 0) { System.out.println("There are no cookies"); } //close firefox browser driver.quit(); } }
Nice Explanation And Nice Solution.Thank u
ReplyDeleteCan I know How to eanble cookies on firefox using webdriver in java?
ReplyDelete