Skip to Content

Postman

2017-10-05

Let's continue our acquaintance with Postman. In the previous article, we considered the main features and only a little bit the testing of requests.

Tests

Testing occurs after receiving a response. You run JavaScript code that checks various response parameters. Here is a simple test:

tests["Response has headers"] = responseBody.has("headers");

where tests - is the object which contains the tests you wrote, which returns true or false,

responseBody - is the object which contains the response body.

Postman also supports EcmaScript 6.

Send the request. After receiving a response, you can see the result of the tests:

In the example the "headers" property in the response body is checked.

You have access to the following global objects:

  • `request` is the data of request,
  • `responseBody` is the body of response,
  • `responseCode` is the code status of response. It contains three parameters: `code`, `name` and `details`,
  • `responseCookies` - cookies. Are available when Interceptor is connected.

There are some generated variables:

  • guid in v4 format - `{{$guid}}`,
  • timestamp - `{{$timestamp}}`,
  • random number from 0 to 1000 - {{$randomInt}},

If an object is expected in the response body, you can work with it by bringing it to JSON format.

let responseJSON = JSON.parse(responseBody);
tests["Value is 100"] = responseJSON.value === 100;

You can use standard functions from the global object `postman` for working with global variables, variables from Postman Environment and cookies:

  • `getGlobalVariable(“param”)` - get a global variable,
  • `setGlobalVariable(“param”)` - set a global variable,
  • `clearGlobalVariable(“param”)` - clear a global variable
  • `getEnvironmentVariable(“param”)` - get the value of a variable from Environment,
  • `setEnvironmentVariable(“param”)` - set the value of a variable from Environment,
  • `clearEnvironmentVariable(“param”)` - clear the value of a variable from Environment,
  • `getResponseCookie(“name”)` - get a cookie.

Example:

postman.setGlobalVariable("result", 100);

let responseJSON = JSON.parse(responseBody);
tests["Result is 100"] = responseJSON.result === postman.getGlobalVariable("result");

postman.clearGlobalVariable("result");

Pre-Request script

Tests are run after receiving a response to the request. But the pre-request script is run before it is sent. The use of this script is obvious: prepare a request before sending, generate data for sending, write data to the Environment or to global variables, etc.

Let's say you need to send a random number each time in a request:

let MAX = 10,
MIN = 1;

postman.setGlobalVariable('randomValue', Math.floor(Math.random() * (MAX - MIN)) + MIN);

We insert the variable `{{randomValue}}` into the body of the response:

Let’s use a request that returns the sent value. Send it. First, a random value is counted, then it will be assigned to the Environment variable which will fit in the request body. Response:

Collection Runner

It would be nice to automate the testing process a little. For example, you need to send requests several times and look at test results. Or you need to check the whole collection at once. For this, Postman has a `Collection Runner`.

In the left part of the window, select the folder with the requests or the whole collection and set the parameters:

  • `Environment` - which environment we need to use the Runner,
  • `Iteration` - number of iterations of running the request/collection,
  • `Delay` - delay before sending each request in ms,
  • `Log Responses` - whether you want to keep a log of all requests,
  • `Data` - a data file
  • `Persist Variables` - whether it is necessary to reset variables to their initial values at each iteration.

Let’s start the Runner. The program will start sending requests one by one with the specified parameters, running tests in parallel for the incoming responses and displaying the results:

In the results window there is a button `Run Summary`, which will display a table with the information of the current run. It contains fewer parts, but is much more compact.

On the right side of the Collection Runner is the history of all executed runs.

If you select any run, Runner will display its results.

Collection Runner is convenient in cases, if you need to periodically check the collection of requests, if you want to check the request / collection with different Environments, etc. The usage process is simple and fast, and the intuitive interface helps you to perceive information easier.

Author: Manar Bakir

Mifort, Mifort-blog, Mifort-articles, Web Development, Developer tool, testing REST API