Environment variables

In mabl, you can define variables at an environment level and use them across multiple tests. Those same tests can then run across multiple environments, such as dev and production, as long as each environment defines the same variables.

A common use case for this feature is to define variables for environment-specific URLs, API endpoints, API keys, etc. so that you can easily run the same tests across multiple environments such as staging and production.

How to create an environment variable

  1. Go to Configuration > Applications
  2. Hover over the name of the environment that you'd like to add the environment variable to. An edit icon will appear next to the environment.
The configuration screen showing to click the title of the environment.The configuration screen showing to click the title of the environment.

The configuration screen showing to click the title of the environment.

  1. Click on the edit icon. The "Edit environment" page will appear.
  2. Click on the "Add environment variable" button.
  3. Enter the variable name and value.
View of the Edit Environment screen showing an Environment variable made titled API_URLView of the Edit Environment screen showing an Environment variable made titled API_URL

View of the Edit Environment screen showing an Environment variable made titled API_URL

👍

Use all caps for environment variable names

By using the conventional "screaming snake case," you can quickly distinguish environment variables from the other variables in a test.
Examples: API_URL, ADMIN_USERNAME

Once created, you can use environment variables in your tests in the same way as you would use other variables created in the test. For example, you can input an environment variable into a text field, use it to visit a URL, or reference it as part of an API request as shown on the screenshot below.

Using an environment variable as part of API requestUsing an environment variable as part of API request

Using an environment variable as part of API request

Random environment variables

By default, when you define an environment variable, its value is treated as a static string. Nevertheless, you can generate random values using string templates with environment variables in the following way:

  1. Create an environment variable using a string template for its value
  2. In the mabl Trainer, create a variable from a string template that is being generated by the environment variable
  3. Run the test from the mabl app to validate everything works as expected

🚧

Limitations

This functionality works as described below but you will not be able to see the actual generated random value in the test run results, unless you review the screenshots.

To create an environment variable that generates a random user email, for example, you can define it with the following string template: user+{{alnum:5}}@example.com (as shown in the screenshot below).

Creating environment variable with a random string template as its valueCreating environment variable with a random string template as its value

Creating environment variable with a random string template as its value

Next, we need to go to our test and, using the mabl Trainer, create another variable, userEmail, that uses the existing environment variable as its string template as shown below.

Creating a variable based on an existing environment variableCreating a variable based on an existing environment variable

Creating a variable based on an existing environment variable

If things are working correctly, you should see the string template assigned to the environment variable in the Preview field.

You can then use this second variable, userEmail, as part of your test. The value of the userEmail variable will not be randomly generated until you run the test in the cloud. When the test runs in the cloud, mabl will assign a random value to the userEmail variable based on the string template coming from the respective environment variable.

In the end, to verify that everything works, just trigger a test run in the cloud and review the results to ensure you see the expected random values as shown in the screenshot below.

Test run resultsTest run results

Test run results


Did this page help you?