API Automation using Postman and Newman, and it’s integration with Jenkins
As you all are aware with the current situation of COVID-19, today utmost important thing is testing. It’s hardly matter how strong you are unless and until your immune system is strong.
Similarly in the software development cycle, testing plays a number of vital roles in delivering quality software. How strong your application’s UI is, it’s always depends on how robust application API’s are.
So whether it’s a virus or a bug, test early and test often.
Hello all my name is Nitin Verma and this walk through will show you how to create collection via Postman and run your API tests using Newman in Node and the command line and also will go through with the integration with Jenkins.
What is an API?
API stands for Application Programming Interface, which is used to stabilize the interaction between two different applications by using any mode of communication.
Prerequisites:-
1. Download, install and logged into postman using email id https://www.postman.com/downloads/
2. Download and install nodejs https://nodejs.org/en/download/
3. Download and install Jenkins https://jenkins.io/download/
- Creating collection and writing test case
- Launch postman application, Click on + New Collection icon

- Provide name and description to your collection. Click Create button

- Click on 3 dots to view more action

- Add request to your collection. Click on Add request
- Provide name to your request

- Provide name to your request and save it
- Open your collection and click on added request
- Enter request URL Eg: https://www.google.com/
- Navigate to Test option and click on Status code: Code is 200 from SNIPPET panel. Javascript call back function will be added

- Click save and execute the request by clicking Send

Bingo you have created your first test case. Though SNIPPET provide you sample of test case (Javascript function), but still you require the basic knowledge of javascript call back function. Also you can test your javascript function using postman terminal (displayed on right hand corner at the bottom)
2. Executing collection using NewmanCLI
Before we start, first install Newman CLI
- Open command prompt
- Command npm install -g newman
- Once installed you can check it’s version newman –version (Optional)
There are 3 options to execute your API’s collection using Newman, first 2 options require manual intervention to get the collection.
A. Option 1
- Navigate to collection in postman, click on 3 dots and click on Export option. Pop will appear click on Export button and save your collection JSON in desired location.
- Now Open Command prompt and execute the command
— Command: newman run <Path of collection>
— Eg: newman run First_Collection.postman_collection.json
— Output:

- Execution report can be exported in different format like cli, junit, JSON and html
- To export html report
a. Install html report package: npm install -g newman-reporter-htmlextra
b. Create a folder where html report will be saved.
Eg: C:/Postman_Automation
Command: newman run <Collection JSON> — reporters cli,htmlextra — reporter-htmlextra-export “C:/Postman_Automation/report1.html”
Execute: newman run First_Collection.postman_collection.json — reporters cli,htmlextra — reporter-htmlextra-export “C:/Postman_Automation/report1.html”

B. Option 2
- Navigate to collection in postman, click on arrow button, click on Share button

- Click on Get public link, copy collection public link

- Now Open Command prompt and execute the command
Command: newman run <Public link> — reporters cli,htmlextra — reporter- htmlextra-export “C:/Postman_Automation/report1.html”
Eg: newman run https://www.getpostman.com/collections/5d1b38f64537d15a89a9 — reporters cli,htmlextra — reporter-htmlextra-export “C:/Postman_Automation/report.html”
Output:

C. Option 3
As the collection is the snapshot of the instance. So every time we change the request or test cases in collection, we need to have updated JSON collection to run our automation (Option 1 and 2). But this require manual intervention whenever test case are changed or modified in every iteration of run.
In this option will use postman API to fetch our collection and execute it through newman.
- To generate collection URL please follow below steps
a. Navigate to URL https://app.getpostman.com/dashboard/integrations this will display your workspace

b. Click on “My Workspace” > Integrations > Browse Integrations

c. Click on Postman API

d. Click on “+ Generate API Key”, provide name to your key and click on Generate API key button

e. Copy API key, Go to Postman create a request and click on Send button
Request URL: https://api.getpostman.com/collections?apikey=$apiKey
Eg: https://api.getpostman.com/collections?apikey= PMAK-5e91f78bada68b00312381e0–83404a74cdfcf7feb8670313b16c0d689a
f. Collection will be displayed

- Will Now Generate the URL for your collection which we will use in newman command
Request URL: https://api.getpostman.com/collections/$uid?apikey=$apiKey
- Execute newman command in terminal
Command: newman run <Collection URL> — reporters cli,htmlextra — reporter-htmlextra-export “C:/Postman_Automation/report1.html”
Eg: newman run https://api.getpostman.com/collections/11009173-95590b92-1a72-4db3-b661-09d0a83fe89b?apikey=PMAK-5e91fb81988efd004220efb2-a22f5381c92afe71bcec489de6f2840dd2 — reporters cli,htmlextra — reporter-htmlextra-export “C:/Postman_Automation/report1.html”
Output:

3. API automation integration with Jenkins.
- Launch Jenkins — http://localhost:8080/
- Go To, Manage Jenkins > Manage Plugin > Available, search and install HTML Publisher plugin and JUnit Plugin and restart jenkins
- Once Jenkins restarted, navigate to New Item

- Name your project, click on Freestyle project > Click OK button
- Your project will be displayed on Jenkins dashboard
- Navigate to your project > Configure > Build > Add build Step > Execute Windows batch command
- Enter your newman command with was generated in Option 3, in command section
Command: newman run <Collection URL> — reporters cli,junit,htmlextra — reporter-junit-export “newman/report.xml” -n 1 — reporter-htmlextra-export “newman/report.html”

- Navigate to Post-build Actions > Add post-build action > Publish HTML reports

- Go to Add post-build action > Publish JUnit test result report, provide Test report XMLs. Click on save

- Click on Build Now to trigger execution

- Build To view Html report click on “HTML Report”

Note: If there is HTML format issue follow below steps
1. Manage Jenkins → Script console
2. And type in the following command:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
3. Then Press Run. if you get the output as ‘Result’, then rerun the build from your project and check HTML report format
Demo Video
Summary
- Postman is one of the most popular tools used in API testing
- It’s always recommended you create an account in Postman, so your collections are available online
- You can provide parameter to your request in Postman
- You can also create Tests to verify a postman request
- Collections can be run using Newman
- Accessibility, Use of Collections, Collaboration, Continuous Integration, are some of the Key features to learn in Postman
Reference