Using Environment Variables

We’ve seen how to pass data to parameterised tests and we’ve seen how data providers can pass many sets of data to run a test many times.

Most of your test data will be benign, such as our test Google search terms of foo and bar.

Some test data is intended to be kept a secret. Perhaps not a nobody-must-ever-know secret, but certainly something that you don’t want defined in the middle of a test or within a data provider.

Defining secrets within your basil code will mean that those secrets will eventually end up in your code repository. Such secrets are irreversibly no longer secret.

A common good practice is to store secrets within environment variables. Let’s see how you can use environment variables within action arguments, assertion arguments and within data sets and data providers.

Creating a Test Requiring User Credentials

# examples/test/example-sign-in.yml
config:
  browsers:
    - chrome
  url: https://www.example.com

"open https://www.example.com":
  assertions:
    - $page.url is $config.url
    - $page.title is "Example Domain"

"sign in as user":
  actions:
    - set $".sign-in-form .user" to "user@example.com"
    - set $".sign-in-form .password" to "password123"
    - click $".sign-in-form .submit"

  assertions:
    - $page.url is "https://www.example.com/account/"
    - $page.title is "Welcome user@example.com"

Defining and Using An Environment Variable

Let’s pretend that some environment variables exist:

export TEST_USER_USERNAME=user@example.com
export TEST_USER_PASSWORD=password123

Referencing an environment variable is very similar to referencing a parameter in a test.

# examples/test/example-sign-in-with-environment-variables.yml
config:
  browsers:
    - chrome
  url: https://www.example.com

"open https://www.example.com":
  assertions:
    - $page.url is $config.url
    - $page.title is "Example Domain"

"sign in as user":
  actions:
    - set $".sign-in-form .user" to $env.TEST_USER_EMAIL
    - set $".sign-in-form .password" to $env.TEST_USER_PASSWORD
    - click $".sign-in-form .submit"

  assertions:
    - $page.url is "https://www.example.com/account/"
    - $page.title is "Welcome $env.TEST_USER_EMAIL"

Prefixing the environment variable name with env. marks the parameter as an environment variable: env.TEST_USER_EMAIL.

Using Environment Variables Within Data Sets and Data Providers

What if we have a set of test users that we want to run through the sign in test?

Here are two examples, one with the data sets defined within the test and a one with the data sets defined within a data provider.

# examples/step/example-sign-in-as-user.yml
actions:
  - set $".sign-in-form .user" to $data.username
  - set $".sign-in-form .password" to $data.password
  - click $".sign-in-form .submit"

assertions:
  - $page.url is "https://www.example.com/account/"
  - $page.title is "Welcome $data.username"
# examples/data-provider/example-sign-in.yml
user1:
  username: $env.TEST_USER_1_USERNAME
  password: $env.TEST_USER_1_PASSWORD

user2:
  username: $env.TEST_USER_2_USERNAME
  password: $env.TEST_USER_2_PASSWORD
# examples/test/example-sign-in-with-data-provider.yml
config:
  browsers:
    - chrome
  url: https://www.example.com

import:
  steps:
    sign_in_step: "../step/example-sign-in-as-user.yml"
  data_providers:
    users: "../data-provider/example-sign-in.yml"

"open https://www.example.com":
  assertions:
    - $page.url is $config.url
    - $page.title is "Example Domain"

"sign in as user (literal data)":
  use: sign_in_step
  data:
    user1:
      username: $env.TEST_USER_1_USERNAME
      password: $env.TEST_USER_1_PASSWORD

    user2:
      username: $env.TEST_USER_2_USERNAME
      password: $env.TEST_USER_2_PASSWORD

"sign in as user (imported data)":
  use: sign_in_step
  data: users