0

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?

HNhu
  • 99
  • 5

2 Answers2

3

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 
Joshua.J
  • 46
  • 3
0

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 email111@gmail.com --value2 11111111

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

Wandrille
  • 6,267
  • 3
  • 20
  • 43
  • I created environment variable ` env : { "param1": "test", "param2": "122121121", } ` and in test file ` cy.get('input[autocomplete="username"]').type(`${Cypress.env('param1')}`) cy.get('input[autocomplete="current-password"]').type(`${Cypress.env('param2')}`) cy.log(`${Cypress.env('param1')} ${Cypress.env('param2')} `) ` and run as `npm run my-script --value1 email111@gmail.com --value2 11111111` but it param1 is test, param2 is 122121121. What var should I create in env? – HNhu May 03 '23 at 09:44
  • Then you should be able to get them with `Cypress.env('XXXX')` – Wandrille May 03 '23 at 09:46
  • I can not pass my param into it, I just update comment – HNhu May 03 '23 at 09:50