Loops and Dynamic Variables in Postman: Part 1

A series that explores how to loop over tests with dynamic data in Postman.

Postman can test APIs by first making a request, then performing assertions against the data returned. In basic cases, requests are processed sequentially and atomically. I had the need, however, to loop through data returned by an API and perform sequential requests and tests using that data. A hopefully simple example of this is below:

  1. GET /api/widgets which returns a dynamic JSON list of widget IDs
  2. Loop over each widget ID, each time calling GET /api/widgets/ and performing assertions on the return data.

Since the IDs of the widgets are unknown to me (and always changing at that), it is impossible for me to statically define these in individual tests. Instead, I want to loop over each widget ID and perform the same assertions, essentially treating the test like a template or function that takes id as a parameter.

I’ll admit before getting too deep in the weeds that this feels hacky and requires an understanding of a few Postman concepts. In this series, I’ll be using RequestBin to mimic an API since it is publicly available and won’t require a dev setup to test with.

Single Test Overview

Let’s take a look at the workflow for a single test:

1. GET http://httpbin.org/get returns

{
  "args": {},
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-us",
    "Cookie": "_ga=GA1.2.1856529796.1487116926; _gat=1",
    "Dnt": "1",
    "Host": "httpbin.org",
    "Referer": "http://httpbin.org/",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0"
  },
  "origin": "1.2.3.4",
  "url": "http://httpbin.org/get"
}

2. Assertions are made on that data:

  • HTTP response code should be 200
  • headers should have have Host set to “httpbin.org”
  • url should be set to “http://httpbin.org/get”

In Postman, the tests would look like:

Example Get Request

Great. Now we know what a basic test looks like. Multiple tests can be grouped in Collections which are just folders of tests to be run, one right after another.

Manipulating Test Ordering and Flow

Now what if we wanted to change the order of tests? Postman introduced a way to do this using postman.setNextRequest('request_name'), where “request_name” is the full name of the test you want to run after the current test completes.

Consider the following tests in the “Test Folder” in our “Loop Example” collection:

Test1 Manipulation

In test1, we could instruct Postman to call “test3” next instead of processing the tests in order and executing “test2”:

Test1 Test Data

Now on running the collection is run, we see that “test3” follows after “test1” and “test2” is skipped altogether.

Test1 Manipulation

Getting and Setting Persistent Variables in Tests

Postman allows for creating and manipulating variables that persist between tests through environment and global variables. Variables can be retrieved in tests by using environment.variable_name and can be set by calling the postman.setEnvironmentVariable('key', 'value') function. Variables can also be used in constructing the request URL using the double curly notation `` (see “test3” below). For example, we could set a variable in “test1”

Test1 Environment Variable

And access it in “test3”

Test3 Environment Variable

Running the collection would render the following in the console output (View > Show Postman Console):

Test3 Environment Variable Output

Lists and Objects as Variables

While variables do not inherently support list and object types, we can work around that by encoding the data first with JSON.stringify() and decoding it later with JSON.parse().

For example, if we wanted to store a list of objects, I could do so using :

var listObjs = [{id: 1},{id: 2}];
postman.setEnvironmentVariable('listObjs', JSON.stringify(listObjs));

Now that list is stored as an environment variable that can be accessed in a test with:

var listObjs = JSON.parse(environment.listObjs);
console.log(listObjs[0].id);

If we wanted to append another object to the list and persist the changes, we could using:

var listObjs = JSON.parse(environment.listObjs);
listObjs.push({id: 3});
postman.setEnvironmentVariable('listObjs', JSON.stringify(listObjs));

Summary

Knowing the basic flow of tests, how to manipulate that flow, and how to pass data between tests, we are now primed to put all these pieces together and dynamically loop tests as though they were functions or templates in part 2 of this series. The entire collection here can be imported by visiting the following link: https://www.getpostman.com/collections/e9dcc10b1b7860eeec45.