- Print
- DarkLight
Passing Variables Between Tests
Overview
When creating tests that follows a customer experience and a business story, you may want to pass values between multiple scenarios in a test plan. For example, you have an order ID generated in scenario A, and you want to enter it into the order management screen in scenario B.
Passing Variables Between Tests makes it possible to pass variables between multiple scenarios run within a test plan, and is developed exactly for such a use case. Let's take a look at how it works.
How it works
Passing Variables Between Tests adds Import variables and Export variables to your scenarios. These variables let you exchange values between tests in a serial execution test plan. They can also be used in parallel execution test plans, but please note that the order in which the values are referenced will be undefined.
Import variables are variables that use values obtained from results of other tests. However, if the expected variable was not imported, a predefined default value will be used. The default value will also be used when the scenario is replayed locally or run individually using "Run now". The value of Import variables can be used in any step that takes arguments, i.e., Input, Text assertion, JS Step, etc.
Export variables, conversely, are defined to make the values obtained in one scenario available in other tests. Each export variable needs to be associated with a step that is returning the value, such as a JS Step or Dynamic Value. Default values cannot be set for export variables. If a value cannot be exported, a blank string is exported instead.
As shown in the diagram, Import variables receive values from Export variables generated in the same test plan. To import a value, the variable must have been exported with the same key as the Import variable.
Import variables do not have to be used immediately, so it is possible to refer to the export variable of any scenario so long as it was run before the target test. If the variable has been exported multiple times with the same key, the most recently exported value will be used.
- Up to 50 variables can be defined for each import and export
- Variables must have unique keys
- Only alphanumeric characters (case-sensitive) and
_
(the underscore character) are allowed in key names - Variable values must be strings only, up to 1000 characters
Set variables in a scenario
Let's take a look at how to set variables. First, after recording a scenario, click on the "Options" tab on the Scenario Details page and click on the "Show Variables Tab".
If you go to the "Variables" tab, you will see a screen where you can add Import variables and Export variables.
Set Export variables
To export variables and make their values available in other tests, your scenario needs to have a step that returns a value (such as a JS Step or a Dynamic Value Step). After a value-returning step has been created, click the [Variables] tab to set variables.
Under the Export variables section, you can specify a Key and a Step’s result as an Export variable. In the Step’s result drop down, you will see a list of steps in the scenario whose output can be used as the value of the Export variable. Note that if your scenario doesn’t have any steps that return a value, nothing will appear in the drop down.
Set Import variables
To use variables that were exported from other scenarios in the current test, you first need to set them as input variables. Under the Import variables section, you can specify a Key and a Default Value for each Import variable. Click [Add] to define each variables that is required. Make sure that the Key for each Input variable matches the Key for the target Export variable.
Passing Variables in action
Example scenarios
Now, let's take the following series of tests as an example to see how Passing Variables works.
Scenario 1
Generate and return the following Export variables:
Username
Phone number
Scenario 2
Receive and use the following Import variables returned by Scenario 1 for Input Steps and Assertion Steps:
Username
Phone number
Return the following new Export variable:
Headline
Scenario 3
Receive and use Username returned by Scenario 1 and Headline returned by Scenario 2 and use them in a JS Step:
Username
Headline
We're going to incorporate these scenarios into a test plan for serial execution to see how it actually works.
Set variables for each scenario
Scenario 1
First, create steps that return the values you wish to use in subsequent tests. Typically, these will be Dynamic Values, or JS Steps that return values retrieved from the screen or an API call. See the following documentation for more information on creating one of these kinds of steps:
Then, go to the Variables screen and add Export variables. Select the appropriate variable key, and set the steps that you just created using the Step's result drop down.
Since Scenario 1 is the scenario that is expected to run first in the test plan, Import variables are not necessary.
Scenario 2
Next, let’s create Scenario 2 to receive the values from Scenario 1. To ensure the correct variables are imported, create Import variables that have the same keys as the Export variables in Scenario 1. If needed, set a Default value for when the scenario is run alone or replayed (otherwise the default of a blank string will be used).
Once Import variables are defined, they will be available for use in the scenario. If you open the details of a step that takes arguments, such as an input step or a text assertion, selecting "Variable" will now allow you to choose an Import variable as the argument.
The following screenshot is an example of specifying an input value for a text field using an Import variable.
Likewise, Import variables can be used for assertions that take arguments.
Let's add an Export variable to this scenario too. This variable will be available to any scenario that comes after this one, but not to any scenario that came before it.
Scenario 3
Just as we have already seen, you can define Import variables that were exported before this scenario run. Scenario 3 should be able to import variables from Scenario 1 and 2.
This means that it is possible to combine the values returned from scenario 1 with the values returned from scenario 2. The following screenshot shows an example of a JS step that takes as arguments the values imported from the result of Scenario 1 and the values imported from the result of Scenario 2.
As shown above, by using variables taken from previous test results, the range of what can be done in a scenario is greatly expanded!
Run a Test Plan
Once the scenarios are ready, create a Test Plan. Add the scenarios in the order they are expected to run, and set it to Sequential.
Once the test plan is ready, run it.
When the test has run successfully, you will see a regular Test Plan Result page. Let’s move on to each Test Result detail.
Result of Scenario 1
When a test uses variables, there will be an Imported Variables section and an Exported Variables section at the top of the Test Result Details page. For the result of Scenario 1, key-value pairs of Exported variables are listed.
Result of Scenario 2
You will see the key-value pairs of variables imported from the preceding test result under Imported Variables section. Right after each key-value pair of Imported Variables, there is a link to the test result where each value is returned. This is useful when you would like to dig deeper into how these values are generated (especially when the test fails).
Result of Scenario 3
The last result is similar to the result of Scenario 2, but only displays the input variables that were used. Again, the link to the right of the value provides a quick reference to each test result that generated the variable.
We hope this has given you a basic understanding of Passing Variables Between Tests. We hope that you will take advantage of this function to create a variety of tests!
FAQs
Is it possible to define variables in Step Groups?
It is not possible to define variables within the step group itself, but within a scenario that uses step groups, it is possible to reference variables and return variables within the step group.
If a variable with the same key is exported multiple times in a test plan, which value is used by subsequent scenarios?
The value of the most recent test result is used. Variables with the same key can be exported any number of times within a test plan.
Is it possible to use the same key for an Import variable and Export variable in the same scenario?
It's possible. The value of the imported variable would be overwritten each time the scenario was run.
What happens to variable references when a test plan is executed against multiple execution environments?
Variables can be passed across execution environments, but may still be overwritten depending on the order in which tests are run.
For example, when Scenario A and Scenario B are in the test plan and Chrome/Linux and iPhone X (Emulator) are specified for the execution environments, the execution order will be as follows:
- Chrome/Linux: Scenario A
- Chrome/Linux: Scenario B
- iPhone X (Emulator): Scenario A
- iPhone X (Emulator): Scenario B
Although Passing Variables can work across execution environments, 4. cannot refer to the value returned by 1. This is because 3. returns the Export variables with the same keys as 1., which means 4. refers to the result of 3.
What happens with Passing Variables when used with data?
Passing Variables also works with test results using data. It is possible to pass variables between execution results of the same scenario using different data if implemented with the same keys in the import and export variables.
What happens when a test that imports a variable is rerun?
The same value is used as in the previous execution.
However, if the Import variables for the scenario are edited after execution, it will work as follows:
- When the default value of the Import variable in use is updated
- The updated default value will be used
- When a new Import variable is added
- The default value will be used
While variables expand the possibilities of a scenario, they can also add another layer of complexity to your tests. If you have any questions or feedback, please feel free to contact us.