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.
Accessing values in an object
If the value of a variable is an object, you can use dot notation to access values from the object in subsequent test steps.
For example, consider a variable called contact_info with the following value:
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com"
}To access a value from that object in a subsequent test step, combine mabl variable syntax with dot notation: {{@contact_info.firstName}}. See the article on working with dates in mabl for more examples of accessing values from an object.
Accessing items in an array
If the value of the variable is an array, use an array index to access a specific item in the array in subsequent test steps.
For example, consider a variable called names with the following value:
["Alice", "Bob", "Charlie"]To access an item from that array in a subsequent test step, combine mabl variable syntax with an array index: {{@names[2]}}.
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.
Truthy and falsy values
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.