Postman cheat sheet - Quick guide to automating APIs

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 Tests tab and some can be found on the right-hand section of the Tests 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://www.getpostman.com/docs.

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

postman.setGlobalVariable("myVariable", "localhost");

Overrides variables of lower scope with the same name

Environmental

postman.setEnvironmentVariable("myVariable", "localhost");

Can be overridden by a global variable of the same name

Reading variables in request builder


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

Reading variables in test tab

Global

var variableToUse = getGlobalVariable("myVariable");

Assigns stored variable “myVariable” to the variableToUse for future use.

Environmental

var variableToUse = getEnvironmentVariable("myVariable");

Assigns stored variable “myVariable” to the variableToUse for future use.

Clearing

postman.clearEnvironmentVariable("myVariable");

Reference: https://www.getpostman.com/docs/postman/environments_and_globals/variables

Status codes

Assert status code matches expected status code

tests["Status code is 200"] = responseCode.code === 200;

Assert status code name

tests["Status code name has string"] = responseCode.name.has("Created");

Headers

Assert header is present

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

Assert header value

var header = postman.getResponseHeader("Content-Type");

tests["Header contains value"] = header === "text/html";

Reponse Bodies

Assert JSON object contains a value

var jsonData = JSON.parse(responseBody);
tests["Assert JSON data"] = jsonData.value === 100;

Assert XML object contains a value

var jsonData = xml2Json(responseBody);
tests["Assert XML data"] = jsonData.value === 100;

Assert HTML page contains a value

var responseHTML = document.createElement("html");
responseHTML.innerHTML = responseBody;

inputs = responseHTML.querySelector(".w3col > ol > li:nth-child(1)");

tests["First item in list is named 'First item'"] = inputs.innerHTML === "First item";

Taken from Parsing HTML responses. This uses CSS Selectors to extract required content. You can discover more on css selectors at w3schools.

Assert x-www-form-urlencoded string contains a value

var queryString = {};
responseBody.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function($0, $1, $2, $3) { queryString[$1] = $3; }
);

tests["Assert urlencode payload"] = queryString["name_of_key"] === "value";

Regex extractor based on code from Steven Benner’s blog post.

Custom logging

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

Conditional workflows

Set next request to run

postman.setNextRequest(“Request Name”);

Exit current run

postman.setNextRequest(“null”);

Based on information from the following Postman blog post