Configure Cypress Tests to Run on Multiple Environments Using Separate Configuration Files

Profile picture for user arilio666

This article will discuss how we can set up multiple environments with separate config files for each environment in cypress.

This approach follows setting up multiple configuration files to run cypress in different environments.

1.) Config Files

Considering two scenarios, prod and stage environments, let us create two config.JSON files in our root.

2.) Config Settings

  • Set up the config files with the following settings and add the respective URLs.
  • Both should contain baseurls pointing to each of the respective sites.
  • Only baseurl changes should be made in each config file, and remaining config settings can be followed from the default config file.
  • This way, we can inherit all settings from cypress.json except baseurl.
//stage-config.json
{
   "extends": "./cypress.json",
   "baseUrl": "https://www.programsbuzz.com"
 }
 
//prod-config.json
{
   "extends": "./cypress.json",
   "baseUrl": "https://www.programsbuzz.com/user/login"
 }
 

3.) Spec File.

  • Create a spec file now and access the baseURl.
  • cypress.config().baseURl can be used to access cypress baseUrl.
describe('Conf Env Test',()=>{
       it('Test',()=>{
        let url = Cypress.config().baseUrl();
        cy.visit(url);
       })
})

4.) Run Tests Via Cypress CLI

Now we can run our specs by specifying the config files we created separately by mentioning them.

npx cypress run --config-file prod-config.json
npx cypress run --config-file stage-config.json

So this is how we can run tests in a different environment having separate config files in cypress.