By storing data extracted from your test environment in variables, you can use the values in subsequent test steps to make your test more flexible and robust. The mabl Trainer supports generating variables from the following sources:
- String template
- Element property
- JavaScript snippet
- Mailbox variable
- Cookie value - browser only
- Page URL - browser only
Other ways to create variables
Other ways to create variables in the Trainer include:
- API steps: extracting the response into a variable.
- Custom find steps: using CSS or XPath
- Test data-driven variables: creating placeholders for data-driven variables
Creating variables from a string template
Use a string template to create a variable from constant values, other variables, random values, or mathematical expressions. Click on {x} > Create variable and set the variable source to "String template."
Enter the string template pattern - see valid expressions for examples. Give the variable a name and click OK to save it.
Creating variables from an element property
If you want to extract a value from the page and store it in a variable, create a variable from element property. Click on {x} > Create variable and set the variable source to "Element property".
Click on the Select button to select an element on the page. Hover your mouse over the target element and click on it.
If the element is difficult to target, try one of these alternative methods to target it:
- Shift + up/down arrows: if the target element is covered by a different overlapping element with a higher z-index, press shift and use the up and down arrows to scroll through overlapping elements at your mouse position until it highlights the element you want to assert on.
- Enter: if any element disappears when you select it with a click, try hovering over it and pressing enter instead.
Select the Property that you want to store in the variable, such as "InnerText" or "Value". Ten assign the variable a name and click OK to save it.
Modifying variable values
If you need to modify the value extracted from the page, create a new variable using JavaScript snippet. To see an example of how to modify a string using a JavaScript snippet, click here.
Creating a variable from a JavaScript snippet
To create a variable from the output of a JavaScript snippet, click on {x} > Create a variable and set the variable source to "JavaScript".
Create a new snippet or use an existing snippet. If you're new to writing snippets, see our article on the Snippet Editor for an overview. To check the output, click Run. Then assign the variable a name and click OK to save it.
Creating a mabl Mailbox address
For email testing scenarios that can be completed in a single test, create a temporary mabl Mailbox address. Click on {x} > Create a variable and set the variable source to "mabl Mailbox address"
Assign the variable a name, and click OK to save it.
Creating a variable from a cookie value
In browser tests, you can store the value of a cookie in a variable. Click on {x} > Create a variable and set the variable source to "Cookie value."
Select the cookie from the dropdown of available cookies. Then assign the variable a name and click OK to save it.
HTTP Only cookies
The list of selectable cookies can include HTTP only cookies, which are commonly used for authentication. HTTP Only cookies are not accessible via JavaScript snippets and can only be accessed in the variable creation menu. For more information on working with cookies, click here.
Creating variables from a page URL
In browser tests, you can store part or all of a page URL in a variable. Use this option to pull dynamic data straight from the address bar, such as an order ID, session token, or search query, and reference it later in your test.
Click on {x} > Create a variable, set the variable source to "Page URL", and capture one of the following parts of the URL:
- Full URL: the complete address.
- Hostname: the domain portion of the URL.
- Path segment: a segment of the URL path.
- Query parameter: the value of a named query parameter.
-
Hash: the fragment after the
#. - Regular expression: a pattern you provide.
When you choose Regular expression as the URL part, the pattern is matched against the full page URL. mabl returns the first capture group, or the whole match if your pattern has no capture group. The /pattern/flags syntax is supported.