What is a cookie?
A cookie is a little piece of data that a web server stores on the client side.
Each time the browser requests a page from the server, this information is transmitted back to the server. Cookies often include customized user data or information.
There can be various use cases where you need to play with cookies, E.g:
1) Maintaining a user’s Shopping Cart.
2) From a Personalized Recommendation functionality perspective, identify the user type and show them personalized products.
And many more….
Type of operations you can perform on Cookies:
1) Retrieve the value of the cookie and verify.
2) Delete the cookie.
3) Add a new cookie.
4) Modify the value of the existing cookie.
How to Test Cookies:
1) Use browser in-built capabilities. Inspect the browser window -> Go to Application -> Open application cookie.
2) You can also add Chrome Cookie Manager Plugin to access application cookies.
How to Automate Cookie Testing:
You can use the Cookie class under org.openqa.selenium package. This class provides access to various methods which you can use to manage the cookies.
1) Adding a cookie:
WebDriver.Options.addCookie(
Cookie
cookie)
Code Example:
public void addCookies(String key, String value) {
try {
remdriver = Constants.remdriver;
value = webPropHelper.getTestDataProperty(value);
Cookie ck = new Cookie(key, value);
remdriver.manage().addCookie(ck);
logger.info("Cookies added successfully");
captureScreenShot(Constants.PASS);
} catch (Exception e) {
captureScreenShot(Constants.FAIL);
logger.error("Failed to add cookies");
getFailedstep(e);
}
}
2) Deleting a cookie:
WebDriver.Options.deleteCookie(
Cookie
cookie)
Code Example:
public void deleteCookies(String key) {
try {
remdriver = Constants.remdriver;
Cookie ck = remdriver.manage().getCookieNamed(key);
remdriver.manage().deleteCookie(ck);
logger.info("Cookies deleted successfully");
captureScreenShot(Constants.PASS);
} catch (Exception e) {
captureScreenShot(Constants.FAIL);
logger.error("Failed to delete cookies");
getFailedstep(e);
}
}
3) Modifying a cookie:
Note: There is not any explicit way to update the cookie value.
You first need to delete the cookie and add it again.
Code Example:
public void upadateCookies(String key, String value) {
try {
Cookie cookie = remdriver.manage().getCookieNamed(key);
remdriver.manage().deleteCookie(cookie);
logger.info("Cookies deleted successfully");
remdriver.manage().addCookie(
new Cookie.Builder(cookie.getName(), value)
.domain(cookie.getDomain())
.expiresOn(cookie.getExpiry())
.path(cookie.getPath())
.build()
);
captureScreenShot(Constants.PASS);
} catch (Exception e) {
captureScreenShot(Constants.FAIL);
logger.error("Failed to update cookies");
getFailedstep(e);
}
}
Bonus:
A sample step def to perform cookie modification:
@Then("^\"(.*?)\" add delete or update the following cookies$")
public void updateCookieValueFromResponse(String user, String type, DataTable cookieTable) {
String cookieName = null;
String cookieValue = null;
String cookieOperator = null;
try {
elements = cookieTable.raw();
for (int i = 1; i < elements.size(); i++) {
cookieName = elements.get(i).get(0);
cookieValue = elements.get(i).get(1);
cookieOperator = elements.get(i).get(2);
if (cookieOperator.equalsIgnoreCase("add")) {
addCookies(cookieName, cookieValue);
} else if (cookieOperator.equalsIgnoreCase("delete")) {
deleteCookies(cookieName);
} else if (cookieOperator.equalsIgnoreCase("update"))
upadateCookies(cookieName, cookieValue);
logger.info("Successfully Performed" + cookieOperator + "operation on" + cookieName);
}
captureScreenShot(Constants.PASS);
} catch (Exception e) {
logger.error("Failed to perform" + cookieOperator + "operation on" + cookieName);
captureScreenShot(Constants.FAIL);
logger.error(getFailedstep(e));
}
}