Easy Postman — just grab and use

mati-qa
9 min readAug 31, 2023

--

Intro

I would say the power of postman, but I’m not sure is it indeed. However, from the QA perspective, for sure it can be used as fast tool for easy API tests… Even for smoke tests. Postman is a fantastic tool made by google. A time ago, you even didn’t need to install it. It was working as browser add-on / extension. Currently, it’s available as a stand-alone tool or a web app (if you create an account or log in). Be aware, that they introduce some limits — so, it’s better to use stand-alone app and not log in. :D

Here you can find the project page. As you can find there:

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs-faster.

But here, I’ll try to show, how to start using the tool for the testing purposes.

Configuration

First things first — of course, before we’ll start using the tool, we need to download it and install — there is also an option to use is as a web application. However, it’s always better to have is locally. As first, we would try to use the app as none authenticated user (without log in to the app).

So, here is the page, where you can find the installation pack. Depends on the platform on which you’re working (Windows, Linux, macOS) — download the installation pack and install it. After installation process will finish, open the app. Below is the screen which you should see if everything go well.

Keep focus, as we would like to check it first as none logged-in user — click on the small link located below the ‘Sign in’ button. This should leads you to the screen as below. Then we can start our adventure.

All we need to make a first request is there. URL input and ‘Send’ button. As nothing else is needed if the endpoint is not restricted. Let’s type e.g. google[.]com as URL and hit Send button. What we can see after that action is presented in the below screen.

  • First entry in the History is shown — there will be added also all requests which you will make — one entry after each Send button hit.
  • Request status section (marked on the screen) — which shows information about the status code, execution time and response size.
  • (Response) Body — which presents all the data which are returned for the request.

There are many more information, like Cookies, Headers, but we’ll skip this at the moment. As I write above, we’ll focus on the testing purposes… So, here is the Test Results tab — which is empty at the moment (you need to believe me, it is ;)). Let’s try to fill it.

Focusing back on the main are (right below the URL input) — we can see the tests tab (screen below) — let’s switch to it.

What we have here:

  • Editor area — which is currently empty — we can write there a code (js code) which will be executed after the response arrived.
  • Snippets — stuff marked with a green frame on the above screen. This is kinda cheat sheet for us — which is very helpful at the beginning of our journey.

The snippets are actually a ready to use pieces of code, which will be added automatically to the editor after you click on its name / description. Among other, there is a code will validate the response code, or if response body contains particular string, etc. Let’s try some of these and send the request with the defaults.

As you can see below, new the Test Results section was filled. There are information about our tests results — as each pm.test() is treated as a single test case.

And mainly, that’s it — this is the easiest use case of the Postman — without any automation and account creation.

Due to latest changes, to go further we need to log in. As for the automation purposes — test runner use or newman use (CLI tool for postman), we need to be able to create a collection. Why collection, because the collection was made to store a bunch of the request. Here is the definition for the official docs:

“Postman Collections are a group of saved requests. Every request you send in Postman appears under the History tab of the sidebar. On a small scale, reusing requests through the history section is convenient. As your Postman usage grows, it can be time-consuming to find a particular request in your history. Instead of scrolling through your history section, you can save all your requests as a group for easier access.” Link to docs

So, let’s go through that — I suggest to use Google account authentication — you can always create a new one if you don’t have already one. After successful log-in to the app, you’ll probably see some weird screen with a bunch of tools which could be added — you can ignore it at the moment and just find somewhere ‘my workspace’ — navigate there. Then using a button marked on the screen below, you should be able to create a new collection.

All right — we are able to create a collection, and rename it. Now we need to add some requests to it, and then we can have a fun with the test runner of the newman. The easiest way to add a request is to right-click on the collection or use the three dot menu next to the collection name (left panel) — take a look on the screen below.

Let’s add three new requests e.g. google[.]com, yahoo[.]com, duckduck[.]com — as a get method. And then, two tests for each — related with the status code, and response body. If all is done, using right-click menu on the collection name, we can go to the test runner — which is hidden by ‘Run collection’ option. Choose that option and see what options we have there.

Of course, not all is for free — in the meaning of infinite usage — so, there are some limitations (and here jMeter is for the rescue). At the moment we are interested with the manual run of the collection in a loop of 3 with a 500ms delay between the requests. All these options are available in the Run configuration area (middle right on the screen above). Let’s set this up, and hit the run button.

What we can see on the above screen — we have a pretty cool overview of our collection run and the tests results. So, now you can modify, add more requests, more accurate tests — you can even create API smoke tests using that tool in a pretty comfortable way. You could even add more logic to is — as you could use the environment variable — to store the credentials, pre-request Script — to generate some random data, or even pass data between the requests. If it comes to the random aka dynamic variables, the postman is really helpful — have a look on the docs.

CLI with the Newman

And now the coolest part — CLI use case. So, kinda workaround for the postman limits. Before we’ll start playing with the command line, let’s export the created collection. For that, again right-click on the collection and click on the Export option.

Leave the defaults (v.2.1) and export the collection to some known place — we’ll use it later after we configure the Newman.

Newman configuration

To use that, we need to have nodejs installed on our machine. Here you can find the nodejs installation pack. I suggest to download and install the recommended one.

  1. Nodejs installation — in most cases default installation process (clicking next, next, next, finish) should be enough. Of course, if you wish, you could customize the installation. After successful installation, open terminal and type the following to check if all is good:

2. Create a catalogue which we will use for our experimentation and navigate to it with the command line (e.g. C:\newman).

3. Now you can continue with installing the Newman pack for the npmjs repository. Here are information about npm i newmanthe package and here is the command which should be used to install the Newman package — type or copy it to the terminal and hit enter:

4. If everything went well, the above screen shows the correct output. The only thing which left, is to copy exported collection to the same directory. Let’s do it.

How to run collection in CLI

Here are a couple useful command which should help:

$ newman run your-collection-name

You may experience the following error (depends on the console you use): “‘newman’ is not recognized as an internal or external command, operable program or batch file.” Then there is a workaround and you can use… The second version of the command is as follows:

$ .\node_modules\.bin\newman run your-collection-name

In the second command, you need to have the direct path to the newman executable file — this is the only difference

Other useful parameters:

  • -r — stand for the reporters which can be used, e.g. cli,json
  • -e <source>, — environment <source> Specify an environment file path or URL. Environments provide a set of variables that one can use within collections — we didn’t touch that here (yet)
  • -d <source>, — iteration-data <source> Specify a data source file (JSON or CSV) to be used for iteration as a path to a file or as a URL. — also didn’t touch here.
  • -n <number>, — iteration-count <number> Specifies the number of times the collection has to be run when used in conjunction with iteration data file — the same as iterations option available in a test runner in Postman UI
  • ‘ — -delay-request, — Specify the extent of delay between requests (milliseconds) — the same as delay option available in a test runner in Postman UI
  • and many more can be found here in the command line options section (somewhere in the middle of the page)

Execution using nodejs form the js file

Here is a piece of code which is needed, if you would like to include it into the CI / CD pipeline — available also in the Newman docs.

const newman = require('newman'); // require newman in your project // call newman.run to pass `options` object and wait for callback newman.run({ collection: require('./sample-collection.json'), reporters: 'cli' }, function (err) { if (err) { throw err; } console.log('collection run complete!'); });

Conclusion

Postman is an awesome tool. Easy to use, to start typing first API tests. I forgot to add, that postman allows to export each request to many languages just like that — using simple option which is here.

However, since I started my adventure with it, it becomes more and more limited. So, if you don’t like to pay, you should use it only for basics, and then switch to other tools. Like e.g. jMeter. I hope, in some other reading, I would be able to demonstrate, how in easy way migrate postman collection to the jMeter. Besides of the restrictions, I really recommend to start having fun with Postman, to become more familiar with the API stuff. To start adding new requests, and more tests to it… to play with dynamic variable — as it could really help to randomize you test data — enviroment variables, or data from imported from files like csv.

--

--

No responses yet