Today let me introduce a man(tool) called Newman , whose is friend (command line companion) of Postman.
Why do we use CLI over GUI?
We should admit that GUI is visually attractive and easily understood. But, for some tasks which are vital, CLI is way more powerful. Here are some reasons,
It needs fewer resources
It ensures high precision
It handles repetitive task easily
It can be Integrate with CI/CD build systems
We can then integrate Postman with your CI/CD build system using Newman, the command-line collection runner for Postman.
Now that you have completely convinced about need of newman , let’s see how to install it !!!
Newman is a tool built by the team behind Postman so that you can run Postman collections from the
command line.
It is also maintained by the Postman team and has feature parity with Postman, which means that you should be able to do anything in Newman that you can in Postman.
However, Newman is not installed when you install Postman, so you need to do it separately.
Installing Newman:
Newman is built into Node.js. So we need to install Node.js first. One of the main feature of Node.js is the package manager. The Node.js package manager, or npm, makes it easy for programmers to publish and share their Node.js code.
To download Node.js go to https://nodejs.org/en/download/ and download node-v16.15.1-x64.msi from LTS as per operating system. Once you have downloaded the installer, run it in the same way you would install any other program on your operating system. You can keep all the defaults on the install wizard.
To verify installations ,open commad prompt and type commands node -v, npm -v.
To install Newman , we can install Newman using npm. By default, npm installs the package so that it is only available in the current folder, but there are options that let you change the scope of where it is installed. The most used of these options is — global (or it’s shortcut, -g), which will install the package globally.
With this command , Newman will be installed and ready for you to use. Please verify installation using newman -v command.
Great job Installing Newman, Let’s see how to use it!!!
Steps to run the postman collection in Newman :
In Postman , create a collection of requests.
Method 1:Export the collection as .json file as shown below.
Once you saved the .json file, open command prompt -> go to the folder where .json file is saved. Use the below command ,
> newman run <filename>.json
Example: newman run SampleCollectionDemo.postman_collection.json
Medhod 2: We can also run postman collection using link. Get the link as shown below.
Once you copied the link , open command prompt and Use the below command,
>newman run <JSONlink>
Example : newman run https://www.getpostman.com/collections/4f97c8e21ca7cfb17e14
Result will be displayed after executing the command . Sample results are as below,
Reporting in Newman :
The point of test automation is to be able to improve a product’s quality based on the results we get. We need reports to summarise how well our tests are doing and list of all failure tests with proper information. Newman has several built-in reporter options. Available reporters are: cli, emojitrain, progress, junitand json.
we can use these option name followed by -r
Example: newman run <filename.json> -r json
Cli reporter is implicit , means even if don’t mention anything in the run command default it display cli report is displayed in console.(as above pic- ‘Run results in console’)
Progress reporter will give you a progress bar in the console while the tests run.
Emojitrain reporter prints out the progress as an emoji, but it is hard to get an emoji to display correctly in a Command Prompt, so it won’t be much use.
json and junit reporters create .json and .xml reports respectively in our working directory under which we are running these commands. All these 5 are built-in with newman ,it allows you to use externally developed reporters.
Along with built-in reporters, Newman allows us to install external developed reporters such as csv reporter,htmlextra,teamcity reporter,confluence reporter etc.
Let’s install htmlextra reporter using below command ,
>npm install -g newman-reporter-htmlextra
We can use it to generate reports with the below run command,
>newman run <collectionfile.json> -r htmlextra
Report will be stored in newman folder in working directory. This will give you a nice dashboard summarizing the results of your run.
Various Run options in newman run :
* Using environments [“-e”] :
We can create environment in postman with common variable used in the collect and export environment as .json file.
we can include this environment in run command as below,
> newman run <collectionfile.json> -e <environmentfile.json>
Example: >newman run SampleCollectionDemo.postman_collection.json -e SampleEnvironmentDemo.postman_environment.json
* Using Data-Driven files [“-d”]:
We can create data files as .csv or .xls and use them in the run with below command,
> newman run <collectionfile.json> -d <datafile.csv>
Example: >newman run SampleCollectionDemo.postman_collection.json -d DataDrivenInputs.csv
* Specific folder [“-folder”] :
We can run only specific folder under a collection using below command,
>newman run <collectionfile.json> — folder <foldername>
Example: newman run SampleCollectionDemo.postman_collection.json — folder GetReqs
* Request Delay [“ — delay-request”] :
We can create delay between request execution by using the below command,
>newman run — delay-request [delayinMilliSeconds]<collectionfile.json>
Example:> newman run — delay-request 5 SampleCollectionDemo.postman_collection.json
* Iteration Count[“-n”]:
We can run the collection multiple times by using the below command,
>newma run <collectionfile.json> -n [numberOfIterations]
Example : > newman run SampleCollectionDemo.postman_collection.json -n 5
* Explore more options :
We have discussed few options , we can see and explore all the run options which we can find using command >newman run -h
Integrating Newman into CI builds :
As mentioned earlier , the real power of running in command line comes in being able to automatically kick off and schedule runs. Let’s see how to integrate with jenkins.
Steps to get started with jenkins,
Go to https://www.jenkins.io/ , download LTS generic package .war file.
Run java -jar jenkins.war , first time when you run you need to give password provided in console and configure jenkins.
Once jenkin is up and running , open the http://localhost:8080/ and log in to jenkins.
Once you login to jenkins → create new item → give the name and select type → ok
Next page ,we can select build → select Execute windows batch command → given the newman run command.
Sometime when we run newman command in jenkins we get unicode characters in jenkin console output , so we are using — disable-unicode option in our run command to avoid those unicode characters.
Then Apply and save.
Now we can run our postman test collections from jenkins.
Conclusion: In this article we learned advantages of using Newman, how to install, run postman collections ,reporting feature , using various run options and finally how to integrate it with CI (jenkins).
Very clearly explained. Nice blog Uma. 👏
Well explained, Easy To understand Blog. Nice work Uma!