If your API testing goals include requirements that cannot be accomplished through mabl’s low-code functionality, you can write custom scripts. In mabl, custom scripts are composed of one or more API snippets, which you can create and reuse across other API tests.
Custom scripts are helpful for handling more complex API testing and troubleshooting scenarios, including:
- 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
Read on to learn more about the types of custom scripts, GenAI snippets, and supported libraries.
Types of custom scripts
Mabl supports adding API snippets to custom scripts at the test and request level:
- Test-level scripts apply to every request in an API test.
- Request-level scripts apply to the individual request.
For a given request, depending on the scripts that exist, the order of execution is as follows:
- Test-level pre-request script
- Request-level pre-request script
- Request
- Response
- Test-level post-request script
- Request-level post-request script
GenAI snippets
If you need help getting started with a custom API snippet, try describing what you want to do with the AI Snippet Generator. Your prompt may include references to test variables and request information. The AI Snippet Generator has access to the following information:
- The name of the step
- Variables in the test
- Request details, including the request method, URL, and headers
- Response details, including the response headers, body, status, and status code
The AI Snippet Generator masks the values of variables, request headers, and response headers that may contain sensitive data. For headers whose name contains “authorization” or “cookie” and variables whose name contains “password”, including the “api.credentials.password” variable, their values are hidden from the AI Snippet Generator.
Supported libraries
Thanks to the mabl integration with Postman, you can use Postman-supported JavaScript objects and functions to handle your custom logic and response validations.
As a best practice, we recommend using the low-code options in the API Test Editor whenever possible and limiting the use of Postman methods to handle more complex scenarios. Tests that don’t use, or make sparing use of, scripts are recommended for two reasons:
- They are easier for all members of a workspace to understand and maintain.
- Their results are easily viewable from the UI.
The Postman integration also makes it possible to use a number of JavaScript libraries and Node.js modules:
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));
JavaScript version support
The API Test Editor supports ECMAScript 8.