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
- File contents - 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". Then 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.
Creating variables from file contents
In browser tests, you can store the text contents of a file downloaded or uploaded earlier in the test as a variable. Use the data from file-content variables to drive subsequent UI steps, API requests, and JavaScript snippets.
Supported files
File-content variables support text-based files, such as JSON, CSV, and XML, up to 10 MB. mabl reads the file when the test runs, so the variable always reflects the file from that run.
Downloaded files
When you save a download step, you can capture the file's contents at the same time. Fill in Save file contents to variable, next to Save as file variable, and mabl saves the downloaded file's text contents to that string variable without a separate step. Note that these two fields do separate things:
- Save as file variable: saves the file for later upload steps
- Save file contents to variable: saves the file's text for later steps
Alternatively, you can create the file-content variable separately after downloading the file: {x} > Create a variable, set the variable source to "File contents," select the download from the File variable list, and give the variable a name.
Uploaded files
Recording a file upload step does not make the file's contents available. To capture them, add the file to the test as a test data-driven variable first: {x} > Manage variables > Data-driven variables > New file variable. See create placeholder variables for data-driven tests for more information.
Then click on {x} > Create a variable and set the variable source to "File contents." Select the uploaded file variable and give it a name.