Cypress – Configuration of JSON File

Cypress configurations consist of some key-value pairs that are applicable to all tests within a framework. Cypress default configurations are available under the Settings tab->Configuration (expand it) in the Test Runner window.

If we look further down in the same window, we shall have the existing values of multiple configurations given by Cypress like the timeouts, environment variables, folder path, and so on.

It is displayed below −

If we look further down in the same window, we shall have the existing values of multiple configurations given by Cypress like the timeouts, environment variables, folder path, and so on.

It is displayed below −

Override Default values

To override the default configurations from the cypress.json file, we have to specify the key-value pairs.

Implementation in cypress.json

The implementation for overriding the default values for the JSON file is as follows −

{
   "baseUrl" : "https://www.google.com/"
}

Here, the key is baseUrl and the value is https://www.google.com/. Once the tests are run again, the changes are reflected in the global configurations, as shown below −

Implementation of Actual Test

The implementation of the actual test for overriding default values of the JSON file is as follows −

describe('Adglob', function () {
// test case
   it('First Test', function (){
      // launch application from configuration
      cy.visit("/")
   });
});

The execution logs show that the baseUrl has been obtained from the cypress.json file and it is applicable to all tests within the framework.

Override Default configurations

We can override the default configurations from the test scripts, which become applicable to an individual test step, within the test case and not to the complete framework.

This is done with the help of the config command in Cypress.

For example, if we want to increase the default timeout for a particular test step, an implementation shall be as follows −

//set default time out to nine seconds from following steps in test
Cypress.config('defaultCommandTimeout',9000)
landPage.selectUser().click()

Simultaneously if the default command timeout value is set to seven seconds in the cypress.json file, then Cypress shall give preference to the timeout applied to the test step(i.e nine seconds).

Finally, it gives preference to the default configurations.

Disable Overriding Default configurations

We can disable the feature to override the default configurations from the cypress.json.

The configuration in cypress.json is as follows −

{
   "defaultCommandTimeout" : "9000"
}

To disable the above configuration, run the below-mentioned command −

npx cypress open --config-file false

After running the above command, the Settings tab of the Test Runner window will show the config flag set to false.

Also, the default command timeout is set to four seconds, which is set by the default configuration and not overridden by the cypress.json value of nine seconds.

Leave a Reply