Cookies is a reserved parameter name for specifying a set of cookies you would like Testim to set for your application.
You can set cookies for each browser session before the test is started or before the page is even loaded.
The cookies parameter is set as an array, where each cookie object needs to contain a name and a value properties.

We have 3 ways you can add cookies to your tests:

  • Test Data 
  • Custom JS (JavaScript) step
  • Configuration File & Run Hooks

Test Data

Use this in the following cases:

  • You need the cookies to be loaded before your system loads.
  • You need the cookies for a particular test.

How to set cookies using dynamic data:

  1. Go to your test editor. 
  2. Click on Setup step (First step in the test) and open the 'Properties' panel.
  3.  In 'Test Data' section, choose 'Set Dynamic Data...'
  4. The JS editor should have opened now.
  5. Set your cookies in the following format:
return {
 "cookies":[{
    "name": "username",
    "value": "tomsmith",
    "domain": "http://google.com"
  },{
    "name": "password",
    "value": "SuperSecretPassword!"
  }]
};

Note:

  • name: Cookie name
  • value: Cookie value
  • domain: Cookie domain (optional)

Custom JavaScript

This option will allow you to set cookies that you need during the test but not immediately at the beginning.

How to set cookies using custom JS:

  1. Go to your test editor. 
  2. Create a custom JS step: Use ‘+’ menu ⇒ 'Add Custom Action'
  3. The JS editor should have opened now.
  4. Set your cookies in the following format:
document.cookie = "username=tomsmith; password=SuperSecretPassword!";

Note:  The cookies will be uploaded after the page was loaded, so if you need the cookies on loading, use one of the other methods.
If you need to add a domain to the cookie, use the Dynamic data. 

Configuration File & Run Hooks

This option will be useful when you need the cookies for your entire test suite.

How to set cookies using configuration file:

  1. Create a JS configuration file
  2. Add the following code to beforeSuite section.
beforeSuite: function (suite) {   
  return {
"cookies":[
        {
 "name": "username",
 "value": "tomsmith",
},
        {
 "name": "password",
 "value": "SuperSecretPassword!"
}]
  };
}

Did this answer your question?