How to manage cookies in WebdriverIO?


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.path - It is an optional parameter and refers to the cookie path. The default value is /(if it is not added while adding a cookie).

  • 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).

  • cookie.expiry.

  • 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
browser.getCookies(['Topic'])
Or,
//to get all cookies
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
browser.deleteCookies(['Topic'])
Or,
//to delete all cookies
browser.deleteCookies()

Example

Code Implementation

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

   //test case
   it('Cookies', function(){

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

      //get a particular cookie
      const t = browser.getCookies(['topic1'])
      console.log(t);

      //get all cookies
      const a = browser.getCookies()
      console.log(a);

      //delete a cookie with name topic2
      browser.deleteCookies(['topic2'])
      d = browser.getCookies()
      console.log(d)

      //delete all cookies
     browser.deleteCookies()
     m = browser.getCookies()
     console.log(m)
   });
});

Updated on: 19-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements