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:
| Protocol | Before | After |
|---|---|---|
| HTTP | Pre-request | Post-response |
| GraphQL | Before query | After response |
| gRPC | Before invoke, On message | After response |
| WebSocket | Before connect | On message |
| Kafka | Before produce | After delivery |
| MQTT, RabbitMQ, NATS | Before publish | After 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:
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.testfor the status code. - Save token: reads the response and saves
tokenfor 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:
| Package | For |
|---|---|
uuid | Unique ids: require('uuid').v4() |
moment | Date parsing, formatting and time windows |
crypto-js | Hashes and signatures: HMAC, SHA-256, Base64 |
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.logoutput 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.