Using variables

Variables help address testing scenarios that require dynamic values. They also help with passing values to, from and in-between test so you can keep your tests shorter and easier to troubleshoot and maintain.

Use cases

  • Generating random values, including email address email testing with mabl Mailbox
  • Extracting values from element properties of the app under test
  • Securely managing user credentials that are passed to a test via variables
  • Calculating dates and deriving other values using custom JavaScript code
  • Performing data-driven testing to easily validate different scenarios
  • Sharing variable values between tests
  • Managing values at an environment level (API keys, endpoints) and using them across tests
  • Applying math calculations on variables and numbers

Variables overview

When working with variables in mabl, you can think about them in three different categories:

  • Environment variables - include system-reserved variables (e.g. app.url and app.username) and derive their values either from the plan configuration or the defined environment variables.
  • Test-generated variables - the value is generated during a test run, as for example extracting it from an element property like innerText.
  • Data-driven variables - the value is derived from a data source such as a mabl DataTable or shared variable from a previous test run.

Adding variables

  1. Start a new trained test and open the mabl Trainer.
  2. Look for and hover over the "{x}" button at the bottom, and click to "Create" a variable.
  3. You will be brought to a new screen where you can assign a dynamic value source to the variable and give it a name. At present, you can generate variables from random string patterns, element properties, custom JavaScript code or a data source.

Adding variables with random values

Creating variables from a string template allows for the ability to use random macros to generate a pseudo-random string. A pattern can contain multiple random macros into which random strings will be substituted. Macros are delimited by double curly braces, e.g. {{macro}}. Currently 5 random string alphabets are supported: alpha (uppercase and lowercase letters), alphaLower (a-z), alphaUpper (A-Z), digit (0-9), and alnum (alphanumeric, i.e. alpha + digit). Along with the alphabet, a length for the generated string must be specified. Here are some examples of patterns and random strings produced by each pattern:

{{digit:2}} inch monitor
87 inch monitor


Each time the test runs a new random value will be generated for each generated variable.

To create a variable with random values:

  1. Enter a name for your variable in the "Variable name" field.
  2. For the "Set value from" field choose "String Template" selector.
  3. Add a desired pattern, using the info located above.
  4. Examine the lower shaded box and ensure it matches your desired output.
  5. Press the "OK" button to save your variable.

Adding variables with basic math operations

Create variables from evaluating basic math operations on numbers or variables. The contents within the {{...}} that includes a math operation will be evaluated into a numerical result and saved as a variable.

The current operations supported are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

Here are some examples of basic math calculations applied to numbers and variables:

{{@numberOne + 2}}
{{3 +2}}
{{@numberOne + @numberThree}}

To create a variable from a math operation and a variable:

  1. Inside the {{...}}, enter the variable @numberFive, an operation (+) and another number (2)
  2. Enter a name for your variable in the "Variable name" field.
  3. Examine the lower shaded box and ensure it matches your desired output.
  4. Press the "OK" button to save your variable.

Adding custom JavaScript variables

Custom JavaScript snippets can be used to generate a variable value. The result will be displayed in the shaded output box. To learn more about this process checkout the "Using JavaScript to Define a Variable" section of the Using JavaScript steps in the Trainer help doc.

Adding element property variables

Property variables are variables whose values are set by finding an element on the page and extracting a property from it. Currently the only property that is supported is the inner text of an element.

To use a property variable, follow the steps below:

  1. Select "Element property" from the "Set value from" selector.
  2. Click the mouse icon.
  3. Select an element on the page.
  4. From the Property selector, choose what element property you would like to set the variable to. e.g. innerText, class, tagName...
  5. Click the "OK" button to store your Element property variable.

Using variables

  1. Once at least one variable has been defined, you will be able to use that variable by clicking on the "(x)" button and then clicking on "Use" in the menu that pops up.
  2. Select whether you'd like to use the variable as an input value or URL to visit. Clicking the latter will automatically add a step.
  1. If you'd like to use the variable as an input value, click the "Input value" button.
  2. Select a text element (an input field or textarea) on the page into which the variable's value should be inserted. Then you will be asked to select a variable to insert.
  1. After clicking "Use variable" the variable's value will be inserted into the selected element on the page.

Using variables within variables

Sometimes it's important to create multiple variables all based on one original variable, whether it be URLs, email addresses, or similar. The ability to add variables within variables make that functionality easy.

  1. Follow the steps above to create your first variable, the process is the same.
  2. Create another variable and assign a new name to it.
  3. Type in the basics for the variable you'd like to create.
  4. Following the standard pattern, type {{@theFirstVariable}} in the spot where you want to add your first variable. For example, a variable called email would be written as {{@email}}.
  5. The box below will update with the variable preview, as shown below.

Using credentials as variables

When adding a plan, if you provide a set of credentials, mabl will exercise the auto-login feature; that is, mabl will try to figure out how to log into the app on its own. But if you want to train instead, the app credentials are now accessible in the trainer as variables, and you can apply them to login fields.

Your credentials are stored separately from the test script. They are encrypted at rest and in transit. You can even configure multiple plans that supply different credentials to the test at runtime.

Credential variables are examples of a new class of variables that are configuration related and automatically defined. You cannot edit the values of these variables, but you can use them like any other variables, including in a generated variable pattern (e.g. "{{@app.url}}") or in a custom JavaScript.

Currently the following variables are supported:

"app.url" - URL of the app under test
"app.defaults.username" - App login username specified in a plan
"app.defaults.password" - App login password specified in a plan
"app.test_run_id" - ID of an individual test run
If you want to use these variables in a custom JS step, you may reference them as:


Watch the next chapter in the quick start video guide.

Did this page help you?