Testim Documentation

Welcome to the Testim documentation. You'll find comprehensive guides to help you get started quickly, example code snippets, and tips for being more productive. You will also find details about advanced features that allow you to customize Testim to fit your environment or special testing conditions. Don't worry, we'll support you if you get stuck. Let's jump right in!

Guides    Changelog

Custom validation and actions

Combining scripts into your test was never easier!

Using JavaScript in Your Tests

Using code in your tests is the best way to make sure they are reusable and easy to maintain.

Testim provides three categories of steps and you can add code to any of them using:

  1. Custom Validations.
  2. Custom Wait-For.
  3. Custom Actions.

Creating a Custom Validation

  • Go to http://www.google.com in the browser.
  • Start a new test, click on the ‘+’ on the right and choose Validations Tab ➔ Add Custom Validation:

Give a name to the new validation and click Confirm.

The editor will open:

This is a Custom (JS) Validation that expects you to return a boolean value. Returning true causes the validation to succeed, while returning false causes it to fail. Note that Custom Wait-For similarly creates a function that returns a boolean value, whereas Custom Action should have no return value.

Examples of passing and failing synchronous validations:

// This validation will always succeed.
return true;
// This validation will always succeed.
return 5 === 5;
// This validation will always fail after it times out.
return 5 === 6;
// This validation will always fail after it times out.
throw new Error('Validation failed!!!');

Go back to the test and run it. We run the following:
throw new Error('Validation failed!!!');

Note that the error message is displayed in the step's result panel at the step's properties panel in case the validation fails:

Creating an Asynchronous Validation via Promise

Have you ever asked out someone on a date?

Sometimes the answer is instantaneous, but on many occasions, that person needs some time to think about it (or at least give the impression of doing so before the inevitable rejection). So you ask: “When will you let me know?” and s/he says “I promise to let you know by 20:00 this evening.” So you go home with that promise in your pocket and this promise seems to you more like a time bomb than anything else.

Let’s take a closer look at what just happened. You made a query and was expecting a boolean return statement (‘return true;’ or ‘return false;’) and instead, you got something like this:

return new Promise(function(resolve, reject) {
  setTimeout(function() {
    if(sheLikesYou) {
      resolve();
    } else {
      reject("You're really a nice person but I’m not ready yet.");
    }
  }, 10000);
});

The promise is given a function that can either resolve or reject.

We waited for some time using the setTimeout function.
The Javascript function setTimeOut receives two parameters:

  • Code to run
  • The wait time (in milliseconds).

Let’s use a promise to test a validation:
Go back into the editor, clear the previous code and type the following:

return new Promise(function(resolve, reject) {
  setTimeout(function() {
    if(7 === 7) {
      resolve();
    } else {
      reject('');
    }
  }, 10000);
});

Run the test - at the end of the timeout the test should succeed.

Note that any text you type into the reject() function invocation will be displayed in the step's property panel in case the validation fails.

Creating a Custom Action

  • Delete the Custom Validation we just made.
  • Click on the ‘+’ on the right and choose Actions tab ➔ Add Custom Action:

In the editor type:

window.location.href = 'https://testim.io/';

Run the test - it should now begin by navigating to the base URL in the Setup (which in our case is http://www.google.com) and then immediately navigate to the Testim homepage.

Chrome Dev Tools Debugger

If you add the line:

debugger;
//Your code

to you JavaScript code, whenever you test is running locally, if your Chrome Dev Tools is opened (Press F-12 (⌥⌘I on Mac) to open it), the Custom step code will stop, and you'll be able to see it in Chrome Dev Tools debugger with all its features like watching variables, step into, step over, etc.

Updated about a month ago

Custom validation and actions


Combining scripts into your test was never easier!

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.