Variables and expressions

Frontend Robot allows you to use variables which can be reused across your tests. These variables can have a static value or a value that is resolved at runtime using functions or simple expressions. Variables can also be pre-assigned at suite level.

Defining variables in a test

To define a variable in a test, use the Assign Variable instruction. The instruction takes as input the the following:

  • A variable name, which can only contain alphanumeric characters and underscores.
  • An assignment type, which can be one of the following:

    • Assign a value: allows you to assign a static value to the variable
    • Assign from element value: assigns the specified element value to the variable
    • Assign from element text: assigns the specified element text content to the variable
    • Assign from element attribute: assigns the specified attribute value of the specified element to the variable
  • An argument, which can be one of the following, depending on the assignment type selected:

    • The variable static value
    • An element selector
    • An attribute name

Take a look at the following screenshot for an example:

Assigning a variable using a static value
Assigning a variable using a static value

Using a variable

You can use variables in most instruction arguments, including selectors. To do so, simply wrap the variable name within ${ and }. For example a variable named todo can be used with the string ${todo}. The following screenshot shows you how a variable can be included in a Send keys instruction:

Using a variable
Using a variable

And below you can see the results of the instruction:

Result of using a variable
Result of using a variable

Using built-in functions

Sometimes you may want to use some data that is defined at runtime. The most common use case is having a random string or the current date to be inserted into a form. You can achieve this by using a set of built-in functions.

The format of a function is almost the same of that of a variable with the addition of two parenthesis () used to specify a comma-separated list of arguments. For example to generate a random string you can use ${randomString()}. To render the current date in the format 2018-01-30 you can use ${now('YYYY-MM-DD')}. The screenshot below shows you an example:

Using functions
Using functions

And below is the result of the above instruction:

Using functions. Result.
Using functions. Result.

You can find here a reference of all the available functions

Using simple expressions in variables

Frontend Robot allows you to use simple logical expressions inside the variable statement. Supported operators are the logical and &&, or || and the ternary operator <condition> ? <then> : <else>.

TIP: to use a default value in place of a missing variable you can use the expression

${variable || "default value"}

Using variables and functions to define other variables

Sometimes it can be useful to use functions and other variables when defining a variable’s value. For example you can define a variable having value Last todo: ${randomSentence()}.

Let’s show an example in the screenshot below:

Using composite variables
Using composite variables

This is what we did in the screenshot above:

  1. We assigned a variable using the randomSentence() function
  2. We used the newly created variable in a Send keys instruction
  3. We reused the same variable in an Element Exists instruction

Defining variables at suite level

You can also pre-define a set of variables at suite level. This is explained in a separate article.

Questions?

We're always happy to help with test creation or other questions you might have! Search our documentation, contact support by email or on chat, or connect with our sales team.