DEV Community

loading...
Cover image for How to change the baseUrl via command line with Cypress

How to change the baseUrl via command line with Cypress

walmyrlimaesilv profile image Walmyr Filho Updated on ・2 min read

Learn a simple technique to perform automated tests in different environments, overwriting the Cypress configuration

Resuming the series "Pinches of Cypress", I decided to bring a simple but recurring theme in discussion forums about testing automation.

Imagine that you need to run the same set of end-to-end tests against a local development environment, in a staging environment, and then in production.

Let's say the URLs for the environments are as follows:

And in the Cypress settings file, the baseUrl property has the value http://localhost:8000. That is, it points to the local development environment.

In the package.json file, we could have the following scripts:

"scripts": {
  "test": "cypress run",
  "test:staging": "cypress run --config baseUrl=https://example.com/my-app-staging",
  "test:prod": "cypress run --config baseUrl=https://example.com/my-app"
}
Enter fullscreen mode Exit fullscreen mode

Finally, at the terminal, or even in a continuous integration pipeline, execute the following commands to run the tests against the different environments.

  • npm test (or npm t - short version) - to run tests against the local development environment
  • npm run test:staging - to run the tests against the staging environment
  • and npm run test:prod - to run the tests in production

That's it!

And just as baseUrl can be overwritten, so can other configurations.


Tell me, what else would you like to see in the series "Pinches of Cypress"?


This post was originally published in Portuguese on the Talking About Testing blog.


Would you like to learn about test automation with Cypress? I was hoping you could get to know my online courses on Udemy. Happy testing! 🎉

Discussion (2)

pic
Editor guide
Collapse
sheenavanpunk profile image
SheenaVanPunk

Useful tip!
Does the same logic work for any parameter from cypress.json file? For example, I have a mailhog url that I would need to alter in different environments.
--config parameter_name

Collapse
walmyrlimaesilv profile image
Walmyr Filho Author • Edited

Hi, @sheenavanpunk ,
I know you can change other configurations via the command line too. I'd recommend giving it a try.