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 the preceding test to share variables
- Train the subsequent test with placeholder values
- Add tests to a plan
- Run the plan in the cloud
Configure the preceding test to share variables
Variable sharing is supported for all test-generated variables, including variables created from the following sources:
- String templates
- Element properties
- mabl Mailbox
- JavaScript snippets
- Database queries
- Cookies
- API responses from UI tests and API tests
For example, if you want to extract the value of an authentication cookie and share it with a subsequent test, create a variable from the cookie value: {x} > Create variable > Cookie value and remember the variable name. If the variable is named cookie_auth, you’ll need to use that exact variable name in subsequent tests that share the variable.
After creating the test that generates the variables you want to share, enable variable sharing:
- On the test details page, click on the edit pencil.
- Toggle on Shared variables.
- Save your changes.
Enabling variable sharing
Limitations
- Data-driven variables, such as values from DataTable scenarios, cannot be shared across tests.
- The combined size of test-generated variables shared from a test must not exceed 1MB.
- Performance tests cannot share variables with other tests.
Train the subsequent test with placeholder values
The next step is to create the test that receives the shared variables. Since the test is not aware of the variables that you want to pass, you’ll need to set up data-driven variables with placeholder values. The variable name must match exactly the variable name from the previous test.
Going back to our cookie example, to use the cookie_auth value in a subsequent test, you’d need to:
- Add a data-driven variable named
cookie_authwith a placeholder value. - Add a set cookie step that uses
{{@cookie_auth}}: + (Add step) > Cookies > Set cookie. This step explicitly sets the cookie to restore the authenticated session.
During training and ad hoc runs, mabl will use the placeholder value. During plan runs in the cloud, mabl will pass the value of the shared variable from the preceding test.
In the Trainer
To create placeholder values in the mabl Trainer, add a data-driven variable: {x} > Manage variables > Data-driven variables.
Adding a data-driven variable in the Trainer
In the API Test Editor
In the API Test Editor, expand the {x} Variables panel to add a variable with a placeholder value. Make sure to add the value to the Default section to ensure they persist after this editing session.
Adding a data-driven 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 the plan to run in the cloud. mabl passes the shared variables from the preceding test to the subsequent test, overwriting the placeholder values. In our cookie example, the actual cookie_auth value from the first test replaces the placeholder, and the set cookie step references the shared variable to restore the session.
View all variable values from a test run
To confirm that everything worked as expected, you can review variables for each test run on the test output page: View all > Variables.
Viewing all variables from a test run
Variable name conflict resolution
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.
Plan settings can impact variable sharing
Some plan settings can lead to unexpected outcomes when variable sharing is enabled.
- Multiple runs: 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. For this reason, we recommend against using shared variables for plans with multiple browsers, DataTable scenarios, or the run multiplier.
- Reruns of failed tests: Plans configured to rerun only failed tests can cause new failures because the rerun might not include essential setup from tests that passed during the original run.