Variables

Variables in Autonomous are helpful for creating dynamic and flexible test scripts. By defining variables in the test steps or capturing them from an API response, you can manipulate data based on the test execution. This allows you to run more comprehensive and robust tests, as the variables can adapt to different scenarios and inputs during testing.

Variables are similar to parameters; call a variable by entering the name of the parameter in braces { }. Unlike a parameter, the variable gets its value when the test is run so the value is not listed in the Test Data window.

Variables can be set explicitly within your test script, derived from text on the screen, obtained from API call responses, or calculated using other variables. This flexibility makes them a powerful tool for testers looking to automate complex scenarios efficiently.

Examples

Below are examples of how you can set a variable:

Command Definition

set email = "email@example.com"

first = "John"

set variable "last" as "smith"

Value is explicitly set in the command so that it can be included in other commands. In the first example the variable {email} is created with a value email@example.com.

user = text to right of "User name"

assign the text to the right of "first name" to the variable "First Name"

Value is taken from text on the screen.In the first example, a variable {user} will be created with a value of the user name displayed on the screen.

FullName = "{first} {last}"

Set "User Name" as "{first}{last}"

Value is derived from other variables which were defined in earlier steps.

set var1 = "my name is {First name} {last name}"

"email" = "{fullname+example.com}

Combination of text and other variables defined in other steps.

Following is an example of a test that uses variables.

Copy
Go to https://sandbox.applitools.com/samples/form
set first = "John"
"last" = "smith"
set fullname = "{first} {last}"
email = "{last}@example.com"
type {fullname} in "Your Name" field
type {email} in "Email" field
Visually check this screen

List Values

A variable can include a list with multiple values. When you call the variable in a test, you can select which value to use or include multiple values. Below are examples of how you can set a variable with a list:

Command Definition

Set variable "Names" as ["Peter", "Paul", "Mary"]

Variable {Names} is created with 3 values.

The command Type {Names} in the "Your Name" field has the result Peter Paul Mary.

Set variable "Names" as ["Peter", ["Paul", "Mary"]]

A list with two values, the first is the string "Peter", the second value is a list with two string values "Paul" and "Mary".

Calling Value From a Variable List

A specific element of a list can be accessed using the [<index>] operator where <index> is a zero-based index. The following examples are based on the variable:

Set variable "Names" as {"First":"Peter", "Last":"Smith"}

Command Definition

Type {Names}

Types the complete variable list:

Peter Paul Mary

Type {Names[0]}

Types the first item in the variable list:

Peter

Type {Names[1]}

Types the second item in the variable list, which is a sub-list

Paul Mary

Type {Names[1][0]}

Types the fist item in the sub-list

Paul

Type {Names[-1]}

Types the fist item from the end of the list, which is a sub list

Paul Mary

Type {Names[2]}

Results in an error as the list contains only 2 items and this call is looking for the thirst item

Dictionary Values

A dictionary value maps string keys to values in a variable. These values may be strings, list values or dictionary values. The value mapped to any can be extracted using the [<key>] or .<key> operator. Both operators perform a case-sensitive match for the key.

The following examples are based on the variable definition:

Set variable "Names" as {"First":"Peter", "Last":"Smith"}

Command Returns

{Name.First}

Peter

{Name[Last]}

Smith

JavaScript ({js-result}) and API responses ({response}), are read-only and are considered as dictionaries. You can apply dictionary operators on them.

If a dictionary is used where a string is expected, it is converted to its JSON string representation.

Regex Expressions

You can extract a value or create a dictionary using a Regex search.

The following examples are based on the variable definition:

Set variable "Var1" as "My name is Mickey Mouse and I'm 10 years old"

Command Returns

{Var1/name is (\S+) (\S+)/}

Generates a list based on the 2 strings after "name is"

["Mickey","Mouse"]

{Var1/\d+/}

Identifies a string with one or more digits

[10]

{Var1/name is (?<firstName>\S+) (?<lastName>\S+)/}

Generates the dictionary:

{"firstName": "Mickey", "lastName": "Mouse"}

API Calls

A Variable can also be generated using an API call or JavaScript. For details, see API Calls with HTTPS Requests and Responses.