Cypress Test Automation: Configure the Framework to Execute Scripts in Multiple Environments

Image source : https://www.neovasolutions.com/2020/04/20/cypress-io-the-future-of-test-automation-tool-for-both-developers-and-qa/

Step 1: Create a file as ‘utility.js’

  • Navigate to the cypress/support folder in your Project
    [Project Folder Root > cypress folder > support folder]
  • Create a Typescript file as utility.js inside the ‘Support’ folder
  • Below is an example of the utility.js file

Step 2: Add code to ‘utility.js’ file to return the URL based on the Cypress environment variables

//utility.js

export class Utility {
getBaseUrl() {
let envi = Cypress.env('ENV'); //Get the value of environment variable i.e ENV
if (envi == 'production') //Check the value
return "https://www.proudction-website.com"; //return desired url
else if (envi == 'staging')
return "https://www.staging-website.com";
else if (envi == 'qa')
return "https://www.qa-website.com";
}
}
  • getBaseUrl() Function : This will return a single URL based on the environment variable value we pass in the package.json file
  • ENV: ENV is an environment variable that will be passed from the package.json file
  • let envi = Cypress.env('ENV'): This will save the value of the Cypress environment variable to a local variable called envi
  • if-else conditions : This will check the value of the environment variable i.e value of ENV in the package.json file
  • if (envi == 'production'):If the ENV=production in package.json file, system will return the production environment URL. Likewise for staging & QA environments.

Step 3: Create a spec file and call the base URL

//test1.spec.js

//Import Utility from support folder
import { Utility } from "../../../support/utility"

//Call getBaseUrl() to get environment specific url value
const url = new Utility().getBaseUrl();

describe('Verify successful login to ABC Portal', function() {
it('User should enter correct credentials', () => {
cy.visit(url); //use url variable
});
});

Step 4: Configure package.json file with environment variables

//package.json"qa:smoke:scripts": "cypress run --spec cypress/integration/Smoke_Suit/*.js --env ENV=qa --browser chrome","stage:smoke:scripts": "cypress run --spec cypress/integration/Smoke_Suit/*.js --env ENV=staging --browser chrome","prod:smoke:scripts": "cypress run --spec cypress/integration/Smoke_Suit/*.js --env ENV=production --browser chrome","qa:smoke:test": "npm run qa:smoke:scripts ","stage:smoke:test": "npm run stage:smoke:scripts ","prod:smoke:test": "npm run prod:smoke:scripts ",

Step 4: Execute the Test Suite in multiple environments as specified

npm run qa:smoke:test
npm run stage:smoke:test
npm run prod:smoke:test

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dilusha Rasangi Kumarage

Dilusha Rasangi Kumarage

Senior Lead — Software Quality Engineering