# How to manage cookies in WebdriverIO?

Rest AssuredDynamic ProgrammingProgramming

We can manage cookies in WebdriverIO. A cookie helps to identify a user. It is an efficient technique to pass information from one site session to another or in between sessions of two connected websites.

We can add, delete and obtain a cookie with WebdriverIO using the methods −

• browser.setCookies - this is used to set a single cookie or multiple cookies for the present page. To set a cookie for a page, we have to first launch and be on that page.

Syntax

browser.setCookies({cookie, cookie.name, cookie.value, cookie.path,
cookie.domain, cookie.secure, cookie.httpOnly, cookie.expiry} )

Here, a cookie is the cookie object or object array and can contain the following values −

• cookie.name - It is an optional parameter and refers to the cookie name.

• cookie. value - It is an optional parameter and refers to the cookie value.

• cookie.domain - It is an optional parameter and refers to the cookie domain. The default value is the present browsing context’s active document’s URL domain(if it is not added while adding a cookie).

• cookie.secure - It is an optional parameter to check if the cookie is secured. The default value is false(if it is not added while adding a cookie).

• cookie.httpOnly - It is an optional parameter to check if the cookie is of type HTTP. The default value is false(if it is not added while adding a cookie).

• browser.getCookies -this is used to get a cookie from the existing page. If the cookie name is provided as a parameter to this method, then that particular cookie shall be obtained. Else, all the cookies from the present page shall be obtained.

Syntax

//to get a specific cookie
Or,
browser.getCookies()
• browser.deleteCookies -this is used to delete a cookie from the existing page. If the cookie name is provided as a parameter to this method, then that particular cookie shall be deleted. Else, all the cookies from the present page shall be deleted.

Syntax

//to delete a specific cookie
Or,
browser.deleteCookies()

## Example

Code Implementation

// test suite name
describe('Tutorialspoint application', function(){

//test case

// launch url
browser.url('https://www.tutorialspoint.com/index.htm')
{name: 'topic1', value: 'WebdriverIO'},
{name: 'topic2', value: 'Selenium'}
])

console.log(t);

console.log(a);

//delete a cookie with name topic2
});