Postman cheat sheet - Quick guide to automating APIs

A quick list of useful functions to use in Postman tests

Postman cheat sheet - Quick guide to automating APIs
A piece of paper with written text on it

Postman cheat sheet is something I have wanted to put together for a while as a response to helping attendees of my Testing API and Web services training. The cheat contains a high-level collection of the features available in Postman to help you to automate APIs and Web services. Unless stated otherwise the methods below are used in the Scripts tab and some can be found on the right-hand section of the tab labelled snippets.

I've organised the Postman cheat sheet based on problems users of Postman are trying to solve based on observations from previous training sessions and questions from Postman slack community. However, if there is something else you want to see added to the Postman cheat sheet, get in touch with me via Twitter @2bittester.

For more detailed information on each feature, check out https://learning.postman.com/docs/introduction/overview/

Using variables

Note: Variables are shared as strings across Postman. This means you need to ensure that whatever you want to store has to be converted to a string before storing. Failing to convert can result in behaviour such as stored parsed response bodies return as [Object object] instead of the original body. You can learn more about sharing response bodies here.

Setting

Global

pm.globals.set("myVariable", "localhost");

Overrides variables of lower scope with the same name

Environmental

pm.environment.set("myVariable", "localhost");

Collection

pm.collectionVariables.set("myVariable", "localhost");

Reading variables in request builder

{{myVariable}}

For example with the variable "myVariable" set to "localhost", using http://{{myVariable}}:8080/ping will create a string of http://localhost:8080

Reading variables in test tab

Global

let variableToUse = pm.globals.get("myVariable");

Assigns stored variable "myVariable" to the variableToUse for future use.

Environmental

let variableToUse = pm.environment.get("myVariable");

Assigns stored variable "myVariable" to the variableToUse for future use.

Collection

let variableToUse = pm.collectionVariables.get("myVariable");

Assigns stored variable "myVariable" to the variableToUse for future use.

Clearing

pm.globals.unset("myVariable");
pm.environment.unset("myVariable");
pm.collectionVariables.unset("myVariable");

Reference: https://learning.postman.com/docs/sending-requests/variables/environment-variables

Status codes

Assert status code matches expected status code

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Assert status code name

pm.test("Status code is 200", function () {
    pm.response.to.have.status("OK");
});

Headers

Assert header is present

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

Assert header value

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type", "application/json");
});

Response Bodies

Assert JSON object contains a value

var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);

Assert XML object contains a value

var jsonObject = xml2Json(responseBody);
pm.expect(jsonObject.value).to.eql(100);

Assert HTML page contains a value

const $ = cheerio.load(pm.response.text());

pm.response.text();
pm.expect($('.w3col > ol > li:nth-child(1)').text()).to.equal('First item');

This uses CSS Selectors to extract required content. You can discover more on css selectors at w3schools.

Custom logging

console.log('Event I am interested in');

Conditional workflows

Set next request to run

pm.sendRequest(“Request Name”);

Exit current run

pm.sendRequest(“null”);

Based on information from the following Postman blog post