Cypress Dynamic BaseURL

Profile picture for user arilio666

The cypress URL can be dynamically used and set as the base URL in the cypress config to load the URL from the config file.

URLs are set in this place and invoked to call the set URL dynamically. In the cypress.config.js, add the following line inside env.

 env: {
   baseUrl: 'https://www.programsbuzz.com/user/login',
 },
  • In the spec file, the baseURL is called like this,
cy.visit(Cypress.env("baseUrl"))
  • For a more dynamic approach, we can follow this.
npx cypress run --config baseUrl=https://example.com/

Or

  • In the package.json, we can set a script like the one below.
"scripts": {
 "test": "cypress run",
 "test:staging": "cypress run --config baseUrl=https://staging-example.com/",
 "test:prod": "cypress run --config baseUrl=https://prod-example.com/"
}
  • Then can directly run.
npm run test:staging
npm run test:prod