Depending on your testing goal, you can leverage JavaScript snippets in the mabl Trainer to:
With these techniques, you can create custom JavaScript steps tailored to your team's specific use case.
Interact with the application
Use JavaScript steps to execute a custom action in your application, such as going back to the previous page in the browser or setting the geolocation.
To create a step that runs a JavaScript snippet, take the following actions in the mabl Trainer:
- Click on the plus icon to add a new step.
- Select JavaScript. The Configure snippet menu will appear in the Trainer window.
- Select an existing snippet or click New to create a new snippet.
- Existing snippet: If it has parameters, update values as needed.
- New snippet: Write your snippet in the Snippet Editor.
- Click OK. The snippet will be added as a test step and run in the Trainer.
Define a variable
If you need to manipulate data in your test, you can store the output of a JavaScript step as a variable. You can create a variable from the output of a JavaScript 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.
- Select an existing snippet or click New to create a new snippet.
- Existing snippet: If it has parameters, update values as needed.
- New snippet: Write your snippet in the Snippet Editor.
- To ensure the snippet outputs the expected value, click Run.
- Give the variable a name.
- Click OK. A new variable step will appear in the Trainer window.
Evaluate a conditional
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.
For more information, check out 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: IF or ELSE IF.
- Choose JavaScript.
- Select an existing snippet or click New to create a new snippet:
- Existing snippet: If it has parameters, update values as needed.
- New snippet: Write your snippet in the Snippet Editor.
- 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.