search

Home  >  Q&A  >  body text

Script command tips for passing environment variables

<p>I have the following cypress nodeJS script command in <code>package.json</code>: </p> <pre class="brush:php;toolbar:false;">"scripts": { "cy:run": "npx cypress run --browser chrome", "cy:parallel" : "cypress-parallel -s cy:run -t 4 -d cypress/e2e/features/ -m false -a '\"--env ENV=${ENVI}\"' " }</pre> <p><code>-a</code> is a dynamic parameter that I want to pass when running the test, such as <code>ENVI=QA</code> or <code>ENVI=LIVE</ code></p> <p>I tried the following but none of them worked for me: </p> <pre class="brush:php;toolbar:false;">SET ENVI=QA npm run cy:parallel</pre> <p><code>ENVI=QA npm run cy:parallel</code></p> <p><code>$ENVI=QA npm run cy:parallel</code></p> <p>Please tell me the correct way of passing parameters from the command line. </p>
P粉295616170P粉295616170536 days ago508

reply all(2)I'll reply

  • P粉465675962

    P粉4656759622023-09-03 10:10:57

    One way is to use the CYPRESS_ prefix.

    To make things simpler (OS independent), use package cross-env.

    Install

    npm install --save-dev cross-env
    

    package.json

    "scripts": {
      "cy:run": "cross-env CYPRESS_ENVI=QA cypress run",
      ...
    }
    

    Test with a simple test:

    it('检查cross-env是否已设置CYPRESS_ENVI', () => {
      expect(Cypress.env('ENVI')).to.eq('QA')                // 通过
    })
    

    reply
    0
  • P粉384679266

    P粉3846792662023-09-03 00:10:34

    Using the following command worked for me:

    Set the value of the environment variable as follows:

    $env:ENVI=QA (on Windows)

    And change the script command as follows:

    Use ENV=%ENVI% instead of ENV=${ENVI}

    "cy:parallel" : "cypress-parallel -s cy:run -t 4 -d cypress/e2e/features/ -m false -a '\"--env ENV=%ENVI%\"'"

    reply
    0
  • Cancelreply