Sharing user credentials with cypress script through gitlab variable

Sharing user credentials with cypress script through gitlab variable


Problem Statement - How to pass user credentials to cypress test scripts through gitlab variable ?


Step by step process to get it done:


Step 1: Set gitlab variables:

navigate to  Gitlab > Settings > CI/CD > Variables and click on Expand

No alt text provided for this image
gitlab

Add User credentials

No alt text provided for this image



Step 2: Install package "dotenv"

 

In Cypress, environment variables (accessible via Cypress.env) doesn't share the same scope as OS-level environment variables. In order to make process.env variables available in Cypress, you should use a third party library, such as dotenv package, which is very popular.

    Use this command to install -    npm install dotenv


Step 3: Make sure this line of code sitting on top of your cypress.config.js

 

                     require('dotenv').config()

 

Add below lines of code as well in cypress.config.js

No alt text provided for this image
cypress.config.js

Note that in Cypress version 10.0.0 and above, setupNodeEvents was added to replace the deprecated plugins file.


Step 4: Export variables in gitlab yml using CYPRESS_* prefix

  - export CYPRESS_TEST_USER=$TEST_USER

  - export CYPRESS_TEST_PASSWORD=$TEST_PASSWORD


No alt text provided for this image
gitlab.yml

 

Make sure that variable are stored in gitlab without CYPRESS prefix.

We are prefixing it while exporting so that it is available to cypress. Anything with Cypress prefix will be available to Cypress.env automatically.


Step 5: Now you can retrieve those variables like this in your cypress script:


Cypress.env("your_proccess_env_property")

No alt text provided for this image
cypress script


Step 6: Running tests on local windows machine after this change

Set the user name and password TEST_USER and TEST_PASSWORD as your system environment variable and it will be available in your script

To view or add a comment, sign in

More articles by zeeshan khan

Explore content categories