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 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:

JavaScript

// This validation will always succeed.
return true;

JavaScript

// This validation will always succeed.
return 5 === 5;

JavaScript

// This validation will always fail after it times out.
return 5 === 6;

JavaScript

// 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:

JavaScript

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:

JavaScript

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:

JavaScript

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 www.google.com) and then immediately navigate to the Testim homepage.

Did this answer your question?