Cookies

A cookie is small piece of information stored on your computer by a website you visit. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user’s previous activity.

Cookies have a certain life span defined by their creators. At the end of this, a cookie becomes expired. Cookies often track information like how frequently the user visits, what are the times of visits, what banners have been clicked on, what button clicked, user preferences, items in shopping cart, etc. This allows the site to present you with information customized to fit your needs.

Cookies are usually used to store information needed for shorter periods. Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose.

Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome) will not be accessed by another browser(Internet Explorer/Firefox).

Some browsers limit the number of cookies stored by each domain(20 cookies). If the limit is exceeded, the new cookies will replace the old cookies.

Where to see Cookies :
Please follow below steps to see cookies in chrome browser.

Lets find the cookies with help of Dora dora-cookies-selenium-webdriver



  1. From the Chrome menu in the top right corner of the browser, select Settings
  2. At the expand left pane of the page, click advanced
  3. Under Privacy and Security, select Content settings tab
  4. Click Cookies
  5. In this page we can select whether to store the Cookie or not for the websites (we can select other options).
  6. Click on the See All Cookies and site data
  7. Now select any website site (domain), one website can set multiple cookies.
  8. Select any cookies, you can see the cookie details, which may look like below.
cookies-chrome-selenium-webdriver

Protractor Interview Questions

WebDriver.Options : Options Interface ( inner interface ) present in the Webdriver interface provides the methods to handle the cookies, methods like creating a cookie, deleting, retrieving a cookie in protractor.

As a reminder, protractor is wrapper around the webdriverJS, so what I said above is right only. Every Class which implements the Webdriver interface have to implement the Cookies handling methods

Below are the methods present in protractor to handle the cookies

We can use the addCookie method to add a specific cookie to the browser. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.

After adding the cookie to a page, we have to refresh the page to load the added cookies.


									addCookie(name: string, value: string, opt_path?: string,
													opt_domain?: string, opt_isSecure?: boolean,
													opt_expiry?: number | Date): promise.Promise<void>;
									

In below example, add some sleep after setting the so that you can manually go and verify in the browser.


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('add Cookie test in Protractor', function() {
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
	});
});

getCookie returns a cookie based on the name of the cookie we pass, getCookieNamed returns null if there is no matching cookies for the name.

If there is matching cookie then getCookie() method will return IWebDriverOptionsCookie interface properties, i have discussed about later in this chapter, as of now know that there is a property called "name", which we are going to retrieve in below example.


								browser.manage().getCookie("cookie name")
								

Complete program for getCookie() in protractor


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('get Cookie test in Protractor', function() {
		console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
		browser.manage().getCookie("webName").then(function(cookie){
			console.log("Name of the Cookie "+cookie.name)
		})
	});
});

getCookies in protractor

Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result


							browser.manage().getCookies()
							

Complete program for getCookies() in protractor


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('get Cookie test in Protractor', function() {
		console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
		browser.manage().addCookie("technology","Future technologies")
		browser.manage().getCookie("webName").then(function(cookie){
			//check last section for different components of cookie
			console.log("Name of the Cookie "+cookie.name)
		})
		browser.manage().getCookies().then(function(cookiesArray){
			for(let i=0; i<cookiesArray.length;i++){
				console.log("Name of the Cookie "+cookiesArray[i].name)
			}
		})
	});
});

Delete a cookie based on its name from the current domain. This is same as setting the named cookie's expiry date to sometime in the past, when the expiry data is crossed browser automatically removes the cookie from the browser.


							browser.manage().deleteCookie("cookie name");
							

Complete program for deleteCookie() in protractor


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('get Cookie test in Protractor', function() {
		console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
		browser.manage().getCookie("webName").then(function(cookie){
			//check last section for diffrent components of cookie
			console.log("Name of the Cookie "+cookie.name)
		})
		// delete cookie
		browser.manage().deleteCookie("webName")
		browser.manage().getCookies().then(function(cookiesArray){
			for(let i=0; i<cookiesArray.length;i++){
				console.log("Name of the Cookie "+cookiesArray[i].name)
			}
		})
	});
});

deleteAllCookies in protractor

Delete all the cookies for the current domain
Warning : If you delete all cookies before completely loading the page, browser may create cookie till the loading completes.

So there is a chance that you may left with some cookies, so better don't delete cookies while loading. Channel ID is not a cookie, so you cannot delete them.


							browser.manage().deleteAllCookies()
							

Complete program for deleteAllCookies() in protractor


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('get Cookie test in Protractor', function() {
		console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
		browser.manage().getCookie("webName").then(function(cookie){
			//check last section for different components of cookie
			console.log("Name of the Cookie "+cookie.name)
		})
		
		browser.manage().getCookies().then(function(cookiesArray){
			for(let i=0; i<cookiesArray.length;i++){
				console.log("Name of the Cookie "+cookiesArray[i].name)
			}
		})
		browser.manage().deleteAllCookies()
		browser.manage().getCookies().then(function(cookiesArrayAfter){
			// prints nothing if deletion of all cookies is successful
			for(let i=0; i<cookiesArrayAfter.length;i++){
				console.log("Name of the Cookie "+cookiesArrayAfter[i].name)
			}
		})
	});
});

IWebDriverOptionsCookie in Cookies with protractor

Components of Cookies :

There are few properties for the Cookie; and few of them are mandatory like name and value, and few are optional, Let's discuss the Cookie's components and how to retrieve them using protractor.

cookies-chrome-selenium-webdriver
In IWebDriverOptionsCookie interface, we have few properties/variables, which will help us to retrieve the values of the Cookie's components, the variables/properties present in the IWebDriverOptionsCookie interface are similar to the components of the Cookie.


							name: string;
							value: string;
							path?: string;
							domain?: string;
							secure?: boolean;
							expiry?: number;
							


Name (mandatory) : This value contains the name of the cookies. We can fetch the name of the Cookie using getName method present in the Cookie class

Value (mandatory) : Value of a cookies will be stored in the content component of the cookie, we can retrieve this values using getValue from the Cookie class.

Path : The server path on which the cookie will be applied. If Path is set to '/java/', the cookie will only be available within the java directory and all sub-directories of the domain. The default value is the entire domain and / sign is used for default. getPath gives the details about path of the cookie

Domain : The domain name allowed to access the cookie, basically the website which sets the cookie. We can retrieve the domain value using getDomain method

Secure : Decides like with connection (HTTP or HTTPS or both) to send the cookies Indicates that the cookie should only be transmitted over a secure HTTPS connection. When set to true, the cookie will only be set if a secure connection exists. We can get the value of the Send for component using isSecure method, return either true or false

expiry : The time when the cookie will expire. values mentioned are in seconds. A value of 0 is equivalent to a 'session cookie': i.e., the cookie expires when the browser is closed, getExpiry method fetches the value of the Expiry Date for the cookie, the return type of the getExpiry() method is Data object.

session cookie : Also called a transient cookie, a cookie that is erased when you close the Web browser. The session cookie is stored in temporary memory and is not retained after the browser is closed.

persistent cookie : Also called a permanent cookie, or a stored cookie, a cookie that is stored on your hard drive until it expires (persistent cookies are set with expiration dates) or until the user deletes the cookie.


import { browser, by, element, ExpectedConditions} from 'protractor'
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('Cookie  COmponents test in Protractor', function() {
		console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
		browser.get("https://google.com")
		browser.manage().addCookie("webName","chercher.tech")
		browser.manage().getCookie("webName").then(function(cookie){
			//check last section for diffrent components of cookie
			console.log("Name of the Cookie "+cookie.name)
			console.log("value of the Cookie "+cookie.value)
			console.log("Path of the Cookie "+cookie.path)
			console.log("Domain of the Cookie "+cookie.domain)
			console.log("Secure value of the Cookie "+cookie.secure)
			console.log("Expiry of the Cookie "+cookie.expiry)
		})
	});
});

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Protractor Training

new tutorial I am starting new Protractor training batch on Weekends. It is two days course from 01-june-2019 to 02-june-2019 based on class room (BANGALORE)

Interested people can confirm the seat by calling to +91 8971673487 or whatsapp to +91 9003381224

Find the course content : View Content