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 |
|---|---|
| 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. |
| 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. |
| Value is derived from other variables which were defined in earlier steps. |
| Combination of text and other variables defined in other steps. |
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:
| Command | Definition |
|---|---|
Set variable "Names" as ["Peter", "Paul", "Mary"] | Variable |
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.
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
| Property | Description |
|---|---|
url | A URL object (see below) with the full page URL. |
status | Numeric HTTP status code (same as {response.status}). |
statustext | Status text (same as {response.status.text}). |
headers | HTTP headers returned for the page (same as {response.headers}). |
cookies | Dictionary of all cookies available for the page and their values (a superset of cookies returned in the HTTP response). |
title | Title of the page as shown in the browser tab. |
width | Full width of the page in pixels (not the viewport width). |
height | Full 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.
| Property | Description |
|---|---|
protocol | Protocol (e.g., http, https). |
user | Username if specified (e.g., user in https://user:pass@example.com). |
password | Password if specified (e.g., pass in https://user:pass@example.com). |
host | Host and port (e.g., example.com:8080). |
hostname | Host name only (e.g., example.com). |
port | Port number (e.g., 8080). |
path | Path after the host and before the query string. |
query | Query string including ? (e.g., ?foo=123&bar=abc). Can be converted to/from a dictionary. |
anchor | Anchor string including # (e.g., #section). |
Syntax Shortcuts
| Expression | Meaning |
|---|---|
{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