Skip to main content

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

  1. 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
  2. You write a test step description with the following text:

    Type {{ my_first_name }} into the first name field
  3. You leave the test step Python code empty so the Qualiti system can generate code for you

  4. You run the test and the system generates the following Python code:

    element = page.get_by_label("First Name")
    element.fill("John")
note

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

  1. 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
  2. You write a test step with the following python code:

    element = page.get_by_label("First Name")
    element.fill(variables.first_name)
  3. You run the test, and the first name field is filled with John

  4. You update the first_name variable's value to Jane

  5. 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

  1. 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
  2. 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
  3. 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}")
  4. You run the test, and the personal information field is filled with Jane Doe, age 43 (age randomly selected)

  5. You run the test again, and the personal information field is filled with Jane Doe, age 91 (age randomly selected)

  6. On the variables page, your saved variable first_name is still John, and there is no saved variable last_name