Using JavaScript with API tests

You can handle complex API testing and troubleshooting scenarios using JavaScript code. Using scripts in an API test can be useful in the following situations:

  • Extracting data from a response in a specific way that requires multiple lines of code
  • Handling specific authentication scenarios, such as getting authentication tokens from third party APIs or checking if auth tokens need to be renewed
  • Executing specific cleanup code after every request in the test
  • Creating assertions or variables from an XML-based response body
  • Running console logs for debugging purposes

This guide outlines how to add pre-request, test, and flow-level scripts to an API test. For working examples, see the API script examples guide.

🚧

Test size limits

Please note that API tests cannot exceed 1MB in size. We recommend using caution with large JavaScript pre-request scripts.

Pre-request scripts

Pre-request scripts execute code before a request is made. To add one, open the API Test Editor and take the following steps:

  • Click on the test step you'd like to edit on the left-side panel.
  • In the Request details section, select the Pre-request tab.
  • Edit your script code as needed.
15081508

Test scripts

Test scripts run code after a request is made. They can be added in the Validation and variables assignment section:

  • In the API Test Editor, click on the step you'd like to edit in the left-side panel.
  • Open up the Validation and variable assignment section.
  • Select the Test tab.
  • Edit your test script as needed.

You can review pre-request and test scripts for individual steps on the Test Details page.

Flow-level scripts

Flow-level scripts are applied on every request in an API test; they include the option for both pre-request and post-request scripts.

To add a flow-level script, click on the pencil icon in the flow title card to open the flow properties modal:

24002400

In the modal, you can select the Pre-request or Post-request tab and edit your script code as needed. Close the modal using the X icon when you are happy with the script.

19381938

Available JavaScript libraries

There are a number of JavaScript libraries and Node.js modules that you can take advantage of when doing API testing with mabl. The availability of these libraries is thanks to the mabl integration with Postman and as such you should be able to work with them as if you were using Postman, for the most part.

JavaScript libraries

  • ajv
  • atob
  • btoa
  • chai
  • cheerio
  • crypto-js
  • csv-parse/lib/sync
  • lodash
  • moment
  • postman-collection
  • tv4
  • uuid
  • xml2js

Node.js modules

  • path
  • assert
  • buffer
  • util
  • url
  • punycode
  • querystring
  • string-decoder
  • stream
  • timers
  • events

You can use the require() method to call one of those libraries. Here is an example of using the moment library to generate a future date that is seven days from today.

let moment = require('moment');
//get a date that is N number of days from today
let date =  moment.utc().add(7, 'days').toJSON();
//specify desired date and time format
let dateFormat = "MM-DD-YYYY"
//format the date and assign it to a variable to be used in other API requests and tests
pm.environment.set("futureDate", moment(date).format(dateFormat));

Learn more

To see specific examples of how to use JavaScript in an API test, refer to the API script examples guide.


Did this page help you?