How to pass environment variable that contains multiple & characters via the command line, in Cypress?

Viewed 69

I am trying to pass the value abc&def&ghi as an environment variable, via the command line, in my Cypress test.

Here is the command I am trying to run:

npx cypress open --env password=abc&def&ghi

When I run the above, I get this console error:

The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.

So I updated my command to have double quotes around the & characters like so:

npx cypress open --env password=abc"&"def"&"ghi

However, with the above command the password is being read as only abc:

enter image description here

I assume I may need to enter an escape character or something like that.

I've tried adding double & single quotation marks around the password when passing it in too, but so far I have only been able to read it in as abc

Can someone please tell me how I can pass abc&def&ghi as an environment variable, so it can be read it correctly?

1 Answers

You have to wrap it in " to send the entire text as a string.

npx cypress open --env password="abc&def&ghi"

Test:

cy.log(Cypress.env("password"))

Command:

npx cypress open --env password="abc&def&ghi"

Test Runner:

enter image description here

Related