javascriptcypress

How to pass Cypress environment variables to cypress-parallel command?


I have one scenario where I need to pass URL, USERNAME and Password for the execution.

Usually I do passing environment variables through command line similar to

npx cypress run --headed --browser chrome --env URL=https://testurl.com,USER=admin,PASSWORD=test

But when I tried to implement cypress-parallel to enable parallel execution, I'm not able to pass Environment variables.

Have provided the below code in package.json

  "scripts": {
    "cy:run": "npx cypress run --headed --browser chrome",
    "cy:parallel": "cypress-parallel -d 'cypress/e2e/ui/FunctionalValidations/' -t 3 -s cy:run"
  }`

and tried to run script with Environment variables it is not accepting the values.

npm run cy:parallel --env URL=https://testurl.com,USER=admin,PASSWORD=test`

How can I pass environment variables to the cypress-parallel method to get executed?


Solution

  • The issue is that npm run cy:parallel does not forward the --env variables to the cy:run script. You need to pass the environment variables inside the cy:run script, not to the cy:parallel command.

    Solution

    Hardcode the --env variables inside cy:run by updating your package.json file like this

    "scripts": {
      "cy:run": "npx cypress run --headed --browser chrome --env URL=https://testurl.com,USER=admin,PASSWORD=test",
      "cy:parallel": "cypress-parallel -d 'cypress/e2e/ui/FunctionalValidations/' -t 3 -s cy:run"
    }
    

    and then just run the following command in your terminal

    npm run cy:parallel
    

    OR, another solution is to pass the variables dynamically from the shell, which I'd prefer. Well, modify the package.json to keep cy:run

    "scripts": {
      "cy:run": "npx cypress run --headed --browser chrome",
      "cy:parallel": "cypress-parallel -d 'cypress/e2e/ui/FunctionalValidations/' -t 3 -s cy:run"
    }
    

    Then, run the following command in the terminal

    URL=https://testurl.com USER=admin PASSWORD=test npm run cy:parallel