JavaScript snippet examples

Examples of JavaScript snippets in use

This guide highlights some examples of JavaScript snippets:

If you need to work with dates, check out our guide on the GetDateComponents snippet: Working with dates in mabl. For even more examples, visit our public JavaScript repo.

Browser back

This snippet calls the browserBackButton function, which simulates hitting the back button in a browser window.

function mablJavaScriptStep(mablInputs, callback) {
    setTimeout(function(){ window.history.back() }, 1000);


Note on setTimeout()

All JS steps run in the current browser context and are complete when the callback() is called. The function setTimeout() tells the current browser context to execute actions after a specific amount of time. These actions will happen after the JS step is complete.

Try it out

  1. In the mabl Trainer, create a JavaScript step.
  2. Copy and paste the code above into the code editor.
  3. Save the snippet.
  4. Click OK. When the snippet runs, it should cause the browser to return to the previous page.

Remove a value from a string

If you want to use a variable value that was extracted from a page element, but the value includes extra characters, you can use JavaScript to remove the characters you don't want.

For example:

  • User ID 5489 can become 5489
  • $45.87 can become 45.87

This snippet takes two parameters, which you'll need to add in manually: "string" and "remove_value":

  • string: represents the full value
  • remove_value: represents the characters in the string that you want to remove.
function mablJavaScriptStep(mablInputs, callback, remove_value = '$', string = '$1.97') {
  // Remove value from string
  string = string.replace(remove_value, "");
  // Return string 

Try it out

Save the output of this snippet to a variable and then use the newly formatted variable value as needed in your test.


Explain parameters in the description

Explaining how parameters work in the description input field can help other members of your workspace understand how to use the snippet.

Learn more

For more examples, visit our public JavaScript repo.