The script editor: snippets, packages and autocomplete

You don't need to remember the script API to write a test. The Scripts tab of a request has autocomplete, a menu of ready snippets and a list of packages you can load. This post is a short tour.

Phases

The left column of the Scripts tab lists when scripts run. For HTTP that's Pre-request and Post-response. Other protocols have their own:

ProtocolBeforeAfter
HTTPPre-requestPost-response
GraphQLBefore queryAfter response
gRPCBefore invoke, On messageAfter response
WebSocketBefore connectOn message
KafkaBefore produceAfter delivery
MQTT, RabbitMQ, NATSBefore publishAfter message

Pick a phase and the editor shows its script. Scripts on the collection and its folders run first, then the request's.

Autocomplete

Start typing cm. and the editor suggests what's there: cm.environment.get, cm.response.json, cm.test, cm.expect and so on, each with a one-line description. The list fits the phase: response calls show up in post-response scripts, and GraphQL and gRPC requests get their own entries.

cm is the script API. pm (Postman's name) and rm are the same object, so these lines do the same thing:

JavaScript
cm.environment.set('token', 'abc');
pm.environment.set('token', 'abc');

Use whichever your team prefers. Scripts imported from Postman keep pm.

Snippets

Snippets below the editor opens a searchable list of ready code for the current phase. Click Insert and it's added to the script. Today's list for HTTP:

  • Set JSON header: sets Content-Type: application/json.
  • Create request id: puts a new UUID in a variable.
  • Assert status 200: a cm.test for the status code.
  • Save token: reads the response and saves token for the next request.
  • Log response: writes the body to the console.

gRPC requests get snippets for metadata and the outgoing message.

Insert example in the toolbar replaces the script with a short commented template for the phase, a good starting point for a new request.

Packages

Scripts can load three packages:

PackageFor
uuidUnique ids: require('uuid').v4()
momentDate parsing, formatting and time windows
crypto-jsHashes and signatures: HMAC, SHA-256, Base64
JavaScript
const moment = require('moment');
pm.environment.set('from', moment().subtract(7, 'days').format('YYYY-MM-DD'));

Packages below the editor lists them with a description; adding one records that the script uses it. The wand button next to it adds the packages a script already requires. Other packages, and Node.js modules such as fs, aren't available: a require of anything else fails with a clear message.

Standard JavaScript works too: JSON, Date, URL, Math, regular expressions, Array methods.

Where to see what a script did

  • Test results in the response panel list every cm.test, failures first.
  • The diagnostics drawer shows console.log output and script errors with the line that failed.
  • A collection run shows both in each request's expanded row.

When a script misbehaves, start with debugging scripts with console.log.