Skip to main content

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:

Following is an example of a test that uses variables.

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:

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"}

CommandDefinition
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"}

CommandReturns
{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"

CommandReturns
{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.

Reserved Variables

page

The {page} variable is a built-in, immutable variable that always contains the most up-to-date status of the current page.

Page Properties

PropertyDescription
urlA URL object (see below) with the full page URL.
statusNumeric HTTP status code (same as {response.status}).
statustextStatus text (same as {response.status.text}).
headersHTTP headers returned for the page (same as {response.headers}).
cookiesDictionary of all cookies available for the page and their values (a superset of cookies returned in the HTTP response).
titleTitle of the page as shown in the browser tab.
widthFull width of the page in pixels (not the viewport width).
heightFull height of the page in pixels (not the viewport height).

URL Object Properties

The {page.url} property value is an object indicating the details of the URL. It can be implicitly converted to and from a string value as required.

PropertyDescription
protocolProtocol (e.g., http, https).
userUsername if specified (e.g., user in https://user:pass@example.com).
passwordPassword if specified (e.g., pass in https://user:pass@example.com).
hostHost and port (e.g., example.com:8080).
hostnameHost name only (e.g., example.com).
portPort number (e.g., 8080).
pathPath after the host and before the query string.
queryQuery string including ? (e.g., ?foo=123&bar=abc). Can be converted to/from a dictionary.
anchorAnchor string including # (e.g., #section).

Syntax Shortcuts

ExpressionMeaning
{page.url}URL of the current page
{page.title}Title of the current page
{page.status}HTTP status code of the page
{page.width}Full width of the page in pixels
{page.height}Full height of the page in pixels

Examples

verify that the page title is "welcome!"
verify that the status of the page is 401
type the url of the page in the text box