javascriptcypresscypress-custom-commands

Cypress: Passing the param into npm script when run by CL


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 --param1=email111@gmail.com --param2=11111111 run-script my-script

I got the error 'error: unknown option: --param1'

Can I pass the param into npm script of Cypress?


Solution

  • 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 user=email111@gmail.com,pwd=11111111 
    

    You use in test

    const user = Cypress.env('user');   // email111@gmail.com
    const pwd = Cypress.env('pwd');     // 11111111