Home > other >  Dynamically set baseUrl in cypress.json
Dynamically set baseUrl in cypress.json

Time:11-11

I have a laravel app and have brought in Cypress testing. In my cypress.json file I have the baseUrl value set to a string of localhost:8000. The issue is the cypress test baseUrl will change based on which environment I am on (Local, Production etc...) so is there a way to set this baseUrl dynamically in the cypress.json file? I ask about the Laravel .env because the baseUrl or APP_URL is already set there.

Thanks.

Current cypress.json

  "baseUrl": "http://127.0.0.1:8000", //How to make this dynamic based on app environment?
  "chromeWebSecurity": false

CodePudding user response:

You can change the value of the baseURL from the cli something like this:

npx cypress run --config baseUrl=https://example.com/

Or, a better approach would be to create multiple commands under scripts tag in your package.json like this:

"scripts": {
  "test": "cypress run",
  "test:staging": "cypress run --config baseUrl=https://staging-example.com/",
  "test:prod": "cypress run --config baseUrl=https://prod-example.com/"
}

And then you can directly run:

npm run test:staging
npm run test:prod
  • Related