Setting Variables
Variables in Test Step Descriptions
When manually creating or editing a test step, you can reference a variable by using the following syntax:
{{ variable_key }}
Example
-
On the
Variables
page, you add a variable with the following values:- Key:
first_name
- Value:
John
- Type: Text
- Description: A consistent first name to use across tests
- Key:
-
You write a test step description with the following text:
Type {{ my_first_name }} into the first name field
-
You leave the test step Python code empty so the Qualiti system can generate code for you
-
You run the test and the system generates the following Python code:
element = page.get_by_label("First Name")
element.fill("John")
Due to current system limitations, the code generated by the system will use the variable's value at the time the code was generated. This means that updating the variable's value will not automatically update the generated code, and subsequent runs of the test will use the old value. To have test runs always use the latest value, you'll need to edit the test step Python code to reference the variable by its key, rather than directly using its value. See also the next section for more information on this.
Variables in Test Step Python Code
When writing Python code for a test step, you can reference a variable by using the following syntax:
variables.variable_key
Example
-
On the variables page, you add a variable with the following values:
- Key:
first_name
- Value:
John
- Type: Text
- Description: A consistent first name to use across tests
- Key:
-
You write a test step with the following python code:
element = page.get_by_label("First Name")
element.fill(variables.first_name) -
You run the test, and the first name field is filled with
John
-
You update the
first_name
variable's value toJane
-
You run the test again, and the first name field is filled with
Jane
Storing Temporary Values Between Steps
You can store temporary values between steps by using the variables
object. Here's how it works:
- At the start of a test execution, the
variables
object is initialized with all variables defined in the project. - During the test execution the
variables
object is mutable, and changes are persisted between test steps. - Any changes made to the
variables
object during a test execution are NOT saved at the end of the test execution.
Example
-
On the variables page, you add a variable with the following values:
- Key:
first_name
- Value:
John
- Type: Text
- Description: A consistent first name to use across tests
- Key:
-
You write a test step with the following Python code:
# step 1: set up variables
variables.first_name = "Jane" # overwrite an existing value
variables.last_name = "Doe" # create a new variable
variables.age = random.randrange(18, 100) # generate a random number -
You write another test step with the following python code:
# step 2: use variables
element = page.get_by_label("Personal Information")
element.fill(f"{variables.first_name} {variables.last_name}, age {variables.age}") -
You run the test, and the personal information field is filled with
Jane Doe, age 43
(age randomly selected) -
You run the test again, and the personal information field is filled with
Jane Doe, age 91
(age randomly selected) -
On the variables page, your saved variable
first_name
is stillJohn
, and there is no saved variablelast_name