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
- File from template - 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 in the same test or shared with another test in a plan
- 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.
Creating a file from a template
In browser tests, you can create a copy of a template file with values from the test filled in, then upload that copy in a later step. Use it to test imports that require different file content each time, such as when your app rejects data it already has, or when a file must reference something the test just created.
In the template, type a placeholder in mabl variable syntax wherever a value goes, such as {{@policyReference}}. Each time the test runs, mabl replaces every placeholder with the value of the variable it names and leaves the rest of the file unchanged.
Supported files
Templates can be .xlsx, .docx, and other zip-based files, or text files such as CSV, JSON, and XML, up to 50 MB. Save text templates as UTF-8. Excel's standard CSV format may not be UTF-8, so choose "CSV UTF-8" instead. In CSV and JSON files, mabl escapes each value so the file stays valid.
Set up the template
- Create your template file. For example, open the spreadsheet your app imports and type a placeholder where each value goes, such as
{{@policyReference}}. Keep each placeholder's formatting the same throughout. If part of a placeholder is bold, for example, mabl can't recognize it. - Add the template to the test as a file variable: {x} > Manage variables > Data-driven variables > New file variable. For example, name it
policyTemplate.
Create the file
- Before the step that creates the file, set a variable for each placeholder. For example, create a variable named
policyReferencefrom the string templatePOL-{{alnum:8}}, so that each run gets a new reference. Variables can also come from the page or from a DataTable. - Click on {x} > Create a variable and set the variable source to "File from template."
- Select the template from the Template file variable list, such as
policyTemplate. - Optionally, enter a File name for the copy, such as
policy.xlsx. If you leave it blank, the copy has the same name as the template. - Give the variable a name, such as
policyFile, and click OK to save it.
To upload the copy, add a file upload step, select File variable as the file source, and select the variable, such as policyFile.
When the step fails
If a placeholder can't be filled, the step fails instead of uploading an incomplete file. The error names the placeholder or the part of the file to fix. The most common causes are:
- A placeholder names a variable that doesn't exist yet. Create the variable in a step before the template step.
-
mabl finds no placeholders. Check that each placeholder is typed as
{{@variableName}}, with the same formatting throughout. - A text template isn't UTF-8. Save it as UTF-8 and upload it to the file variable again.
- A CSV value contains a comma, quote, or line break, and its placeholder shares a field with other text. Put the whole field in double quotes, or give the placeholder a field of its own.