skip to Main Content

I tryna pass the param param1 and param2 when I run the script by command Line.

Here code in test.cy.js

it('Clean data', () => {

    const param1 = process.env.npm_config_param1;
    const param2 = process.env.npm_config_param2;

    before(function () {
        cy.visit('/')

        //Login
        cy.get('#username').type(param1)
        cy.get('#password').type(param2)
        cy.get('#loginBtn').click()
 })

I defined script in package.json

 "my-script": "npx cypress run --spec "test.cy.js" --param1 value1 --param2 value2"

I run in CL

npm [email protected] --param2=11111111 run-script my-script

I got the error ‘error: unknown option: –param1

Can I pass the param into npm script of Cypress?

2

Answers


  1. You should define them as environment variable and it’s possible in command lines (see documentation).

    In your case, it would be:

    "my-script": "npx cypress run --spec "test.cy.js" --env param1=$value1,param2=$value2"
    

    and

    npm run my-script --value1 [email protected] --value2 11111111
    

    You can also refer to this How to use variables in npm scripts on windows

    Login or Signup to reply.
  2. Double-dash is correct way to pass params to inner script, but it needs a space before it.

    This answer Sending command line arguments to npm script shows good example code.

    You define script in package.json

    // no "placeholder" params on the script
    "my-script": "npx cypress run --spec "test.cy.js""   
    

    You run in CLI

    npm run-script my-script -- --env [email protected],pwd=11111111 
    

    You use in test

    const user = Cypress.env('user');   // [email protected]
    const pwd = Cypress.env('pwd');     // 11111111 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search