Steps To Add Delete and Update cookies using WebDriver in Automation
Posted By : Sachchidanand Tripathi | 15-Jun-2017
Some times we need to handle cookies for testing purpose while testing web services based application.
Steps to create , update and delete cookies are as:
Step 1:
Import cookie class like as:
|
|
import org.openqa.selenium.Cookie; |
Step 2
Retrieve all cookies like as:
|
|
private Set<Cookie> getAllCookies() { //-------Get all cookies using this line of code ------- return driver.manage().getCookies(); //-------Get name based cookie ----------- return driver.manage().getCookieNamed(name); //-------Get value based cookie ----------- return driver.manage().getCookieNamed(name).getValue(); } |
Step 3
Create cookies like as :
|
|
//-------Single cookie ----------------------------------------------------------------------- private void addCookie(String name, String value, String domain, String path, Date expiry) { driver.manage().addCookie( new Cookie(name, value, domain, path, expiry)); }
//-----Multiple cookies creation------------------------------------------------------- private void AddingCookies(Set<Cookie> allCookies, String domain) { for (Cookie c : allCookies) { if (c != null) { if (c.getDomain().contains(domain)){ driver.manage().addCookie( new Cookie(name, value, domain, path, expiry)); } } } driver.navigate().refresh(); } |
Step 4
Delete cookies like as :
|
|
//This method deletes a specific cookie private void deleteCookieNamed(String name) { //Single cookies driver.manage().deleteCookieNamed(name); //All cookies driver.manage().deleteAllCookies(); } |
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sachchidanand Tripathi
Sachchidanand Tripathi is having a good experience over selenium.