QA Wolf logo

QA Wolf

/

Star

2.7k

Use Environment Variables

Environment variables are secret values that you can use in your test code. Because they are encrypted, they are useful for storing sensitive values like passwords.

Environment variables can also be used to change the behavior of your test by group. For example, you may want to write a test that goes to one URL for staging and a different URL for production.

In this tutorial, we'll learn how use environment variables in our tests.

Create an Environment

Environments are groups of environment variables. You can create, rename, and delete environments from the test editor and from the dashboard.

Let's create an environment if you don't have one already. In our example, we'll create an environment called "Staging".

Open the environments popup by clicking the "Environments" button in the sidebar of the dashboard, or the configure button in the bottom left corner of the test editor.

Edit environments from dashboard
Edit environments from test editor

Create an environment by clicking the "Create environment" button on the left side of the popup.

Create an environment

Type the environment name in the input and press enter to save your environment.

Save an environment

You can rename and delete environments by hovering over them and clicking the options button.

Edit an environment

Create an Environment Variable

Now that we have an environment, let's add some variables to it.

Click the "Create variable" button in the right side of the environments popup to create a variable.

Create environment variable

Enter the variable name and value into the form. We'll name our varaiable TODO_TEXT and give it a value of secret todo. Click "Save" to save your variable.

Save environment variable

Your variable will be encrypted and saved. You can edit and delete environment variables by hovering over them and clicking the relevant button.

Edit environment variable

Click "Done" to close the popup.

Use an Environment Variable

We can now use our TODO_TEXT environment variable in our test. Environment variables are accessible under process.env.

First, open your test in the editor. Make sure your desired environment is selected in the bottom left corner of the test editor.

Choose environment in editor

In our example, we'll replace a hard-coded todo value with the value saved as TODO_TEXT (secret todo). We'll change this line of code:

await page.fill('[placeholder="What needs to be done?"]', "todo");

To this line of code:

await page.fill(
'[placeholder="What needs to be done?"]',
process.env.TODO_TEXT
);

In the code editor, we can delete the existing todo value ("todo") and replace it with process.env.TODO_TEXT. You'll notice as you type process.env. that your environment variables will be shown as suggestions.

Use environment variable in editor

If we run our test, it will now create a todo with the value secret todo, which comes from our environment variable.

Run a test with an environment variable

Assign Environment to Trigger

Environments can be assigned to triggers in QA Wolf. This means that when tests runs are created by the trigger, they will have access to the variables in the assigned environment.

To add an environment to a trigger, open the triggers popup.

Add trigger from dashboard

Edit the trigger so that your desired environment is selected.

Edit trigger environment