Selenium Webdriver
can perform required task with respect to browser cookies. We add , delete,
delete particular cookie by passing the name, and so on. Let us look all of the
in details.
Add Cookie:
driver.manage().addCookie(cookieName);
This method is used 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.
driver.manage().addCookie(cookieName);
This method is used 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.
Delete Cookie
driver.manage().deleteCookie(cookie);
This method is used to delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.
driver.manage().deleteCookie(cookie);
This method is used to delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.
Delete Cookie with Name
driver.manage().deleteCookieNamed(cookieName);
This method is used to delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.
driver.manage().deleteCookieNamed(cookieName);
This method is used to delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.
Delete All Cookies
driver.manage().deleteAllCookies();
This method is used to delete all the cookies for the current domain.
driver.manage().deleteAllCookies();
This method is used to delete all the cookies for the current domain.
Get Cookies
driver.manage().getCookies();
This method is used to get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result.
driver.manage().getCookies();
This method is used to get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result.
Get the Cookie with Specific Name
driver.manage().getCookieNamed(cookieName);
This method is used to get a cookie with a given name. It will return the cookie value for the name specified, or null if no cookie found with the given name
driver.manage().getCookieNamed(cookieName);
This method is used to get a cookie with a given name. It will return the cookie value for the name specified, or null if no cookie found with the given name
You’re
now ready to write some code. An easy way to get
started is this example
import
java.util.Set;
import
org.openqa.selenium.Cookie;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
public
class FindAllCookiesInaWebSite
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get(http://techyworks.blogspot.in/”);
Set<Cookie>
cookies=driver.manage().getCookies();
//To find the number of cookies used by this site
System.out.println("Number of
cookies in this site "+cookies.size());
for(Cookie cookie:cookies)
{
System.out.println(cookie.getName()+" "+cookie.getValue());
//This will delete cookie By Name
//driver.manage().deleteCookieNamed(cookie.getName());
//This will delete the cookie
//driver.manage().deleteCookie(cookie);
}
//This will delete all cookies.
//driver.manage().deleteAllCookies();
}
}
0 Comments