Categories
Software | June 6, 2023

How to run Postman collections with Newman: The hack trick

In the digital world (and in the real world, as well), finding ways to do something quicker is like scoring a midfield goal.

To delve into the world of APIs, sometimes we find ourselves forced to repeat the same process repeatedly to obtain certain information. But then, what do we do with that information? We often get a result, and that’s it. But in other situations, we need to process it like a good midfielder, project it towards a certain destination, and get a detailed report of the “play.”

Postman emerges as a great tool to work with APIs, and now, with the handling of other types of requests.

In Postman, we can define and organize the entire request environment into folders and functionalities. But what happens when we want to process different plays scenarios to cover the required functionalities? Although this tool has a runner that allows us to drag and drop to create a scenario with a succession of requests for execution, it falls short for request reuse and the creation of new paths.

That’s where Newman (a great last name for a striker) comes into play. It’s a magnificent tool that allows you to run Postman collections along with their environment variables from the command line. 

Installation is simple. With NPM already installed on the system, we just need to type in the console:

And once we have exported the request collection from Postman, we can run it with:

If we want to add a list of environment variables, we can export them from Postman and run them with:

As simple as that. Point for Newman, and the score is 1-0..

Now, if we want to enrich our tests, both for QA and Devs, there are more parameters and plugins that make this possible.

Did you know that it’s possible to work dynamically with the location of the collection and its environment variables? You no longer need to have the physical file exported from Postman. To reference them, all you need is to obtain some IDs.

And why not apply this to our continuous integration tests as well, since we can have this service running in an unattended mode?

Let’s get the ball rolling! From Postman, we need to share our collection from the Share -> Via API option and obtain the public link.

Then, we need to replace the file path with the collection.

Similarly, for environment variables, we can extract the ID and use it as a parameter.

Attention:

To use the collection URL, you must use the access_key you get when sharing the collection or the API key provided by Postman.

For the URL formed for the environment, the API key must be passed without fail.

Both of the following base URLs can be used:

The URLs would be formed as follows:

For the collection:

  • https://api.postman.com/collections/{{YOUR_COLLECTION_ID}}?apikey=YOUR_API_KEY
  • https://api.postman.com/collections/{{YOUR_COLLECTION_ID}}?access_key=YOUR_ACCESS_KEY

For the environment:

  • https://api.postman.com/environments/{{YOUR_ENVIRONMENT_ID}}?apikey=YOUR_API_KEY

Therefore, the final URL would be formed as follows:

newman run ${COLLECTION_URL} --environment ${ENVIRONMENT_URL}

Goal! Newman wins 2-0.

And as we approach the 90th minute, in order to take full advantage of the tool, we can add it as a library to our Node project, giving us even better control of the ball.

By installing a few libraries, we can unlock its full potential.

npm i newman
npm i newman-reporter-htmlextra
npm i dotenv

We can run multiple executions in parallel, build our sequence script of test cases, customize reports, switch between environments easily, and store results for later analysis.

That’s a goal from the midfield for a resounding 3-0 win.

Great to hear that Newman has proven to be a valuable addition to your toolkit! It’s a versatile tool that can make testing and automation tasks easier and more efficient. Its ability to execute collections from the command line, integrate with Node.js projects, and work dynamically with collections and environments makes it a powerful tool for developers and QA professionals alike.

“With this hat-trick or hack-trick, Newman takes the ball home.”

By Mariano Sckerl

QA/QC at Patagonian. I bring extensive experience in Testing. I'm drawn to automation testing and techie stuff. I am a music enthusiast and that is what keeps me grounded.

Leave a Reply

Your email address will not be published. Required fields are marked *