POSTMAN with Newman

Newman is used as a command-line collection runner in POSTMAN. It allows us to run a test in POSTMAN directly from the command line. Also, a whole collection can be run using Newman in the command line interface. It is built in such a way that it can be easily integrated with your server(continuous integration server) and with build systems.

Newman maintains the property of feature parity with POSTMAN and allows you to run collections a similar way, as you do inside the POSTMAN native app in your system. The advantage lies in the fact that, even when the POSTMAN app is not in action or not executing, then also the test can be run using the command line.

Newman resides inside the NPM registry and on GitHub.

Newman is built on Node.js and therefore for using the Newman, Node.js should be installed into the system. Therefore, the steps are:

  • Download and install NodeJs.
  • Install Newman.
  • Start running the collection in the command line.
Download NodeJs

Visit the link: https://nodejs.org/en/download/ and you must see different versions available for NodeJs.

postman-node-js-downoad-page

Click on the 64-bit system the download will start. It should not take more than two minutes to download the software if the internet speed is good enough.

postman-nodejs-software-downloaded

Once you click on the software, it will ask for permission to start installing.

postman-nodejs-download-confirm

Now click on next and start installing the software. The software will not ask for any dependencies or actions, instead, it will install all the necessary tools required to use the nodejs. Such as Python, etc.

Once the software has been installed into your system, open the command prompt in your local machine. And type the following command:

npm install -g newman

postman-install-newman-cmd

After running this command, the Newman along with all other tools will be installed into your computer.

Newman will be installed only of NodeJs was successfully installed into the system.

Export Collections

To run a collection through the command line using NodeJs, you must export the collection and then execute it later through the command prompt.

To export a collection, go to the collection and hit the three dots:

postman-collection-export-option

As you click into the Export option, you will get different formats for exporting the collection.

postman-collection-json-export

As you can see in the above image, there are three different options for exporting the Collection. The third one shows the recommended. This is recommended by the POSTMAN software itself, so it's better to select this always if you are not sure about the usage of others.

Selected the collection by activating the radio button beside it, and then click into the Export button.

postman-collection-saving-directory

As you can see that the path of the directory, I am choosing is: Local Disk->Users-> HP, because, this is the path which will open in the command prompt, by default. And so, we can directly run the Collection in the same path using Newman. Then save the file there. I have named as: my-exported-collection, you can use any name.

Running the Exported Collection

Now, as you have saved your collection, let us see how to run the collection using Newman.

Go to the command prompt:

postman-command-prompt-demo

Now, to run the collection, type: newman run collection_name

So, in my case:

newman run my-exported-collection.json

postman-collection-run

After a few seconds, you should be able to see your collection has started running. And you get the response.

collection-testing

→ list-all-users
  GET https://reqres.in/api/users?page=2 [200 OK, 1.68KB, 849ms]

→ list-single-user
  GET https://reqres.in/api/users/2 [200 OK, 698B, 242ms]

┌─────────────────────────┬─────────────────────┬────────────────────┐
│                         │            executed │             failed │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              iterations │                   1 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│                requests │                   2 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│            test-scripts │                   0 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│      prerequest-scripts │                   0 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              assertions │                   0 │                  0 │
├─────────────────────────┴─────────────────────┴────────────────────┤
│ total run duration: 1139ms                                         │
├────────────────────────────────────────────────────────────────────┤
│ total data received: 1.19KB (approx)                               │
├────────────────────────────────────────────────────────────────────┤
│ average response time: 545ms [min: 242ms, max: 849ms, s.d.: 303ms] │
└──────────────

Customized Running of Collection

In the above example, you can see that when we run the collection with the name, then the collection simple gets executed with all the requests it has inside it.

Now, it is also possible to customize the running of your collection, which means, you can select a particular environment while running, change the number of requests you want to send, etc.

File Uploading using Newman

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now