How to run your cypress test across multiple environments


When running test with Cypress there can be situations where the same test needs to be run across multiple environments such as QA Server and Staging Server to assure the quality of the release. This blogpost I will show one simple way to achieve this at run time.

There are multiple ways in which we can achieve this, one such way is to use --config at run time and pass the required server URL.

Our first task should be to add the below line to our cypress.config.js within the scripts block. As shown below we pass the QA server and Staging server URLs with the cypress run command and point to our spec file

"scripts": {

 "cy:stage:runall": "cypress run --config baseUrl=https://app.stage.test.com/ --spec cypress/e2e/app/*.cy.js --browser chrome",

 "cy:qaserver:runall": "cypress run --config baseUrl=https://app.qa.test.com/ --spec cypress/e2e/app/*.cy.js --browser chrome",

  }, 

Once this is configured we can then run the command npm run cy:qaserver:runall on the command line which will then invoke all the test pointing to the QA Server and by executing the command npm run cy:stage:runall the cypress test will get invoked pointing to the staging server url.

Alternatively, if we want to run the test with a Github action we can configure as below by giving the respective script name. Below is a section of the Github actions script.

- name: Run Cypress Test :Run all test on QA Server

        if: always()

        uses: cypress-io/github-action@v4

        with:

          working-directory: app

          command: npm run cy:stage:runall


Alternatively, we can give ./node_modules/.bin/cypress run --config baseUrl=https://app.stage.test.com/  within the command line which will achieve the same results. If you want to confirm that the proper URL is called instead of run give open to see the test runner open with the given URL.

Comments

  1. welcome, hope this will be useful to help run the test on multiple environments

    ReplyDelete

Post a Comment