In mabl, you can share variables between tests within the same plan. Shared variables are great for setting up scenarios that pass data-driven variables across test runs, such as:
- Storing an authentication cookie in a variable and passing it to other runs
- Creating dynamic test data in one test and passing it to another test for validation
- Generating test data, such as user IDs or emails, in one test and deleting them in a different test at the end of the plan
This article explains how to set up variable sharing between tests in a plan:
- Configure a test to share variables
- Manually create placeholder variables
- Add tests to a plan
- Run the plan in the cloud
Configure a test to share variables
Create the test that generates the variables you want to share, and enable variable sharing:
- On the test details page, click on the edit pencil.
- Toggle on Shared variables.
- Save your changes.
Enabling variable sharing
Variable sharing is supported for all test-generated variables, including from the following sources:
- String templates
- Element properties
- Mabl Mailbox
- JavaScript snippets
- Database queries
- Cookies
- API responses
Variable size limit
The combined size of test-generated variables shared from a test must not exceed 1MB.
Manually create placeholder variables
The next step is to create the test that receives the shared variables. Since a test is not aware of the variables that you want to pass to it, you need to manually add the shared variables as data-driven variables with default values. These placeholder variables serve as placeholders for training and ad hoc runs.
For example, if you want to pass a variable named user_id
to this test, add a data-driven variable named user_id
with a placeholder value to use for training and ad hoc runs. The variable names must match the names of the variables that are being passed onto the test. Names are case sensitive.
In the Trainer
To create placeholder values in the mabl Trainer, add a data-driven variable: {x} > Manage variables > Data-driven variables.
Adding a placeholder variable in the Trainer
In the API Test Editor
In the API Test Editor, add placeholder variables in the variables panel. Make sure to add the value to the Default section to ensure they persist after this editing session.
Adding a placeholder variable in the API Test Editor
Add tests to a plan
The final step is to add the tests to the same plan. Configure the plan so that the test that has shared variables enabled finishes execution before the test with placeholder variables. There are two ways to set this up:
- Run sequentially: Place all tests in a sequential stage that runs the tests in the given order.
- 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.
Run the plan in the cloud
Trigger your plan to run in the cloud. mabl passes the shared variables from the first test to the subsequent test, overwriting the placeholder values created for training and ad hoc runs.
Variable name conflict resolution
If multiple test runs generate and share the same variables, but with different values, the last test run that completes will override the variable values set by prior test runs. As a general rule, avoid sharing variables from tests with the following plan configurations:
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 variable (default values)
- Flow data-driven variables (default values)
The value coming from the DataTable scenario will override other values.