Introduction
In this article, we will look at the Newman CLI Tool.
Pre-requisite
Command-line interface is a means of interacting with a computer program (or software) by typing line by line commands in your shell (command prompt or terminal).
Newman
- Newman is a command-line runner for Postman collections. In other words, it allows a user to run an existing Postman collection through the command line.
- Newman takes the JSON version of the collection that can be obtained by simply exporting the collection in JSON collection format or the URL of the collection which is nothing but the same JSON that’s obtained by the collection export.
Installation
To install Newman we need to have node installed in your system
Then use npm and install Newman
npm install -g newman
Running Postman collection in CLI
In order to run the Postman collection in CLI
Navigate to the folder which contains the collection JSON file through CLI.
Now run the collection using,
newman run <provide json file name>
newman run collectiontwo.json
Screenshot of the collection run log,
If you want to run your collection with respect to number of times then use the below CLI command.
n represents Iteration value set to 5. It means that the requests inside the collection will execute five times.
newman run jsonfilename -n value
newman run collectiontwo -n 2
Screenshot of the collection run log when run using n flag. You can see the iteration 1/1 and 1/2
If you want to run a particular folder inside the collection then use the below cli command,
newman run <collection_name> --folder <folder name>
Delays are the time intervals between execution of each API hit. So a delay of 2 seconds will be given against Each API hit. We will try to achieve the same through Newman by following the steps.
newman run <collection_name> --delay-request 5000
newman run collectiontwo.json --delay-request 5000
Screenshot of the collection run when ran using --delay-request flag
You can see the run duration time when we ran using delay flag in the above screenshot.
We used the environment variables in Postman, we can also set the environment variables in Newman.
newman run <collection> --environment <file>
We need to export the environment and refer it in the above command
To export the environment go to top right hand corner environment section,
Click on the eye icon, you will see a table open with the environment variables displayed click the export option which exports the environment as a JSON file.
Screenshot of the section where you can export the collection
If you want to run your collection where your data should be driven from external files like CSV and excel then use the below cli command
newman run jsonfilename -d filename
Real-Time usage of Newman
Postman Newman is built to easily integrate it with your build systems and continuous integration server.
If You want to integrate your postman tests in third-party integration tool like Jenkins then these commands will be helpful
In Jenkins you can create a freestyle project,
In the Add build step section of your Jenkins project, we can provide the cli command with help of windows/batch cli options.
The below command will help you with different flag options which help you in running your postman collection.
newman run -h
To know the version of newman use the below command
newman -v
Summary
This article will help you with concepts of newman and where exactly we can use it in our Test Automation.
Thank you