Working with snippets
How to create and use snippets in browser tests
This guide provides an overview of the different ways you can create, import and use snippets in browser tests.
API scripts
JavaScript snippets are only used in browser tests. If you are looking for information on using JavaScript in API tests, click here.
Creating snippets
You can create snippets in two different areas of the application:
- In the mabl Trainer
- On the Snippets page
In the mabl Trainer
To create a new JavaScript snippet step in the Trainer, take the following actions:
- Click on the plus icon to add a step.
- Select JavaScript. The Configure snippet menu will appear in the Trainer window.
- Click on the New button.
- Write your snippet in the Snippet Editor. You may wish to run the snippet to check that it works as expected.
- Click Save. You will have the option to save your snippet as a one-time or reusable snippet.
- Click OK. The snippet will be added as a test step and run in the Trainer.
One-time vs. reusable snippets
One-time snippets are used once in the test and cannot be imported to other tests. Reusable snippets can be used more than once in a test and can be imported into other tests.
It is also possible to create new snippets for variable steps and conditional steps.
The Snippets page

In addition to creating snippets in the mabl Trainer, you may also create snippets outside the Trainer on the Snippets page:
- Click on Tests from the left-hand navigation.
- Select the Snippets tab.
- Click on the + New snippet button.
- Write your snippet in the Snippet Editor.
- Click Save.
Note
Keep in mind the following when creating or editing a snippet from the Snippets page:
- Snippets cannot be run outside the mabl Trainer. (The Run button is absent from the Snippet Editor in this mode.)
- Snippets created from the Snippets page are always saved as reusable snippets.
Importing snippets
Using an existing snippet in the Trainer
You can import an existing snippet in your test by taking the following steps:
- Click on the plus icon to add a step.
- Select JavaScript. The Configure snippet menu will appear in the Trainer window.
- Click on the snippet dropdown and select the snippet you wish to use.
- If there are parameters, update as needed.
- If you want to ensure the snippet works as expected, click Run.
- Click OK. The snippet will be added to the test and run in the Trainer.
Creating a new snippet from an existing snippet
If you would like to modify a snippet and keep the original, you can take the following steps:
- Click on the plus icon to add a step.
- Select JavaScript. The Configure snippet menu will appear in the Trainer window.
- Click on the snippet dropdown and select the snippet you would like to modify.
- Update the snippet in the Snippet Editor.
- Click on the down arrow next to the Save button and select "Save as new reusable snippet." This creates a new snippet from the existing snippet.

Using snippets
You can use snippets in three different ways in a browser test:
- As a standalone step (also called a JS step)
- To define a variable value
- To evaluate a conditional (IF steps, ELSE IF steps)
Creating a JS step
- Click on the plus icon to add a step.
- Select JavaScript. The Configure snippet menu will appear in the Trainer window.
- Create a new snippet or use an existing snippet.
- Click OK. The snippet will be added as a test step and run in the Trainer.
Using JavaScript to define a variable
You can create a variable from the output of a JS snippet by taking the following steps:
- Click on the {x} icon in the bottom of the Trainer window.
- Click on Create a new variable.
- Select JavaScript as the source.
- You can create a new snippet or use an existing snippet.
- If you want to ensure the snippet works as expected, click Run.
- Give the variable a name.
- Click OK. A new variable step will appear in the Trainer window.

Using JavaScript for a conditional step
When a snippet is used in the context of a conditional, the result of the snippet will be evaluated for truthiness to determine whether the condition is met.
- If the result of the snippet is a truthy value, the steps inside the conditional block will run.
- If the result of the snippet is a falsy value, the steps inside the conditional will be skipped.
Truthy and falsy values
For more information, check on the MDN glossary for truthy and falsy values.
Follow these steps to use JavaScript for a conditional step:
- Click on the plus icon to add a step.
- Click on Conditionals.
- Select the conditional statement you'd like to add.
- Choose JavaScript.
- Create a new snippet or use an existing snippet.
- If you want to ensure the snippet works as expected, click Run.
- Click OK. A new conditional step will appear in the Trainer window.
After recording that step, add the steps that should occur if the conditions of the JavaScript step evaluate to true.
Updated 8 months ago