WebdriverIO - Cookies



We can handle cookies using 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.

Methods for Cookies

We can add, delete and obtain a cookie with WebdriverIO using the following 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 stay on that page.

Syntax

The syntax is as follows −

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

Here, 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

The syntax is as follows −

//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

The syntax is as follows −

//to delete a specific cookie
browser.deleteCookies(['Topic'])

Or,

//to delete all cookies
browser.deleteCookies()

To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO which are as follows −

Step 1 − Install NodeJS. The details on how to perform this installation are given in detail in the Chapter titled Getting Started with NodeJS.

Step 2 − Install NPM. The details on how to perform this installation are given in detail in the Chapter titled Installation of NPM.

Step 3 − Install VS Code. The details on how to perform this installation are given in detail in the Chapter titled VS Code Installation.

Step 4 − Create the Configuration file. The details on how to perform this installation are given in detail in the Chapter titled Configuration File generation.

Step 5 − Create a spec file. The details on how to perform this installation are given in the Chapter titled Mocha Installation.

Step 6 − Add the below code within the Mocha spec file created.

// 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) 
   });
});

Run the Configuration file - wdio.conf.js file with the following command −

npx wdio run wdio.conf.js 

The details on how to create a Configuration file are discussed in detail in the Chapter titled Wdio.conf.js file and Chapter titled Configuration File generation.

The following screen will appear on your computer −

Cookie

After the command has been executed successfully, first the cookie details having the name as topic1 get printed in the console. Then, both the cookie details having names as topic1 and topic2 get displayed.

The following screen will appear on your computer −

Cookie Screen

Then, we deleted the cookie with the name topic2, so the other cookies got printed in the console. Finally, on deleting all the cookies, an empty array is printed in the console.

Advertisements