Let's say you want to pass an environment variable with the user name into a Cypress test. Since the test runs in the browser, you cannot access the
process.env.USERNAME directly. Instead you need to inject the variable using on of the several of the ways. Here is how I would do it:
Use .as-a.ini file
I would keep all passwords in a file
.as-a.ini that is ignored by my source rules. Thus the sensitive information is never checked into the source control. For example, I would name the block of environment variables
To inject these variables when starting Cypress, I would use my utility tool as-a that you can install globally or as a dev dependency
npm i -g as-a
Cypress automatically reads all environment variables that start with
CYPRESS_ prefix and makes them available in
// spec file
You can also get each variable separately
// spec file
Watch how I use the
as-a tool in this short video "Use Utility as-a To Load Multiple Secrets And Pass To Cypress"
Tip: read my blog post Keep passwords secret in E2E tests to make sure the tests don't reveal sensitive information.
Pass an entire object
If an environment variable
CYPRESS_ has a JSON stringified object, it will be automatically decoded into an object. For example, let's put the user information into
Now let's read this object when launching Cypress Test Runner
as-a user npx cypress open
You can see the entire parsed object in the
env object in the projects settings tab
The test can confirm the object has what we expect to find:
// show full objects in the assertions message
Tip: want to confirm the types of the properties and not the values? Use cy-spok assertions.
This example comes from the repo bahmutov/cypress-as-a-example and is shown in the short video "Inject Entire Objects Into Cypress Tests Using as-a Utility"