Sharing variables between tests

Using mabl, you can share variables between tests within the same plan.

Use cases

  • Creating test data with randomly generated variables such as user IDs or emails that you would like to delete in a different test at the end of the plan
  • Generating variables in one test that you would like to then validate in a different test in the same plan such as creating a user and then assigning it to different roles

Setup

At a high level, you can take the following steps to share variables between tests:

  1. Create the test that generates the variables you would like to share and configure that test to share variables.
  2. Create other tests that will use the shared variables and add data-driven variables with default values as placeholders to these tests.
  3. Add all those tests to the same plan.

When run in a plan, the variable values will get passed from the test that has variable-sharing enabled to the tests that are expecting these values.

Configuring a test to share variables

You can share all variables that generated their value during a test run. We refer to those variables as test-generated, and they can be from the following sources:

  • string templates
  • element properties
  • mabl mailbox
  • JavaScript steps
  • API steps
582582

Creating a variable from a random string template

Enable variable sharing

  1. Go to the Test Details page of the test that generates the variables you'd like to share.
  2. Click on the edit icon.
23262326

Edit test details

  1. Enable variable sharing and click "Save."
12341234

Enable sharing variables

🚧

Variable size limit

The combined size of test-generated variables shared from a test must not exceed 1MB.

Create data-driven variables with placeholder values

The next step is to create the test that receives the shared variables. Since a test does not know what variables it will receive on execution, you need to create and use data-driven variables with default values as placeholders.

📘

Use the same variable name

The variable names must match the names of the variables that are being passed onto the test. Names are case sensitive.

There are two ways to create test data-driven variables:

  1. Variables > Create variables > Set variable using Data source
  2. Variables > Manage the variables in this test > Add variable
584584

Creating a data-driven variable named "user_id"

When you run the test in the mabl Trainer or trigger an ad hoc test run, the test will use the placeholder values.

Create a plan

To run the tests and share variables between them, you need to also add them to the same plan.

In order to pass variables from one test run to another test run, ensure that the test that passes the shared variables runs prior to the tests that expect those variables. There are two ways to set this up:

  1. Run sequentially: Place all tests in a sequential stage sequential stage that runs the tests in the given order.
  2. Use stages: Set up the plan so that the tests that share variables are in one stage and the tests that expect those variables are in subsequent stages.

When the plan runs, mabl will overwrite the default values with values from the variables passed onto the test, as long as the variable names are an exact match.

Variable name conflict resolution

If multiple test runs generate and share the same variables, but with different values, then the last test run that completed would override the variable values set by prior test runs. This is also true if the plan has multiple browsers, run multipliers and/or the test has a DataTable with multiple scenarios associated with it.

If multiple values are passed to a test for the same variable, the following order takes precedence: DataTable Scenario > Shared variable > Environment variable > Test data-driven variables (default values) > Flow data-driven variables (default values). The value coming from the DataTable scenario will override other values.

👍

Important considerations

Sharing variables between tests in a plan works best when the plan runs in a single browser on a single environment and without a run multiplier to ensure that the tests sharing variables run just once within the plan.


Related resources