Test 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 simple variable in a test, simply use the Assign Variable instruction. The instruction takes two arguments a name and a value.

A variable name can only contain alphanumeric characters and underscores.

Take a look at the following screenshots for an example:

Assign a variable
Assign a variable

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()}.

In the screenshot below we:

  1. Assign a variable using the randomSentence() function
  2. Use the newly created variable in a Send keys instruction
  3. Reuse the same variable in an Element Exists instruction

Using composite variables
Using composite variables

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.