Postman Automation with Newman

 Introduction

Newman: 

The tool that allows us to make the bridge between the Postman App and any CI/CD server is called: Newman. Newman is the CLI companion of Postman,and it can execute Postman collections (as files or from links).

Newman Installation:

Prerequisite  : Node.js (Java script Run time environment)
Link to download :  https://nodejs.org/en/download/package-manager/
> node --version
to check Node.js is downloaded or not 

To install Newman :

https://www.npmjs.com/package/newman
>npm install -g newman
>newman --version
if getting error as :
 "File C:\Users\xyzz\AppData\Roaming\npm\newman.ps1 cannot be loaded because running scripts is disabled on
this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ newman --version
+ ~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess"

Step To resolve this issue :

1.Open PowerShell as an Administrator:
Right-click on the PowerShell icon and choose "Run as Administrator."
Check the Current Execution Policy:
Run the following command to see the current execution policy:
2.Get-ExecutionPolicy
Change the Execution Policy:
If the current policy is set to "Restricted," you can change it to "RemoteSigned" or "Unrestricted" (depending on your security requirements). Use the following command to change the execution policy:
3.Set-ExecutionPolicy RemoteSigned
If you are prompted for confirmation, type Y and press Enter.
Re-run the Newman Command:
After changing the execution policy, try running the newman command again:
4.newman --version
Reset Execution Policy (Optional):
After you've completed your tasks with Newman, you may want to reset the execution policy to its original state for security reasons. To do this, run:
5.Set-ExecutionPolicy Restricted

Running collection from URL

GET : https://api.postman.com/collections/{collection_id}
Copy collection_id 

Need to specify Apikey

Link to generate API Key : https://identity.getpostman.com/accounts?intent=switch-account&target_team=web&continue=https%3A%2F%2Fgo.postman.co%2Fsettings%2Fme%2Fapi-keys

Add apikey as below:






Re run again this time we will get status code :200 ok with collection details means this endpoint is working fine now we can use this command on newman command line .

https://api.postman.com/collections/17978790-e8c27790-4bf8-455b-bb90-4b5c1c424c02?apikey=PMAK-656ac7d835341966d37ff0f9-c69b28b1075a764deb10584ea0b61b8e








Command to use 

> newman run https://api.postman.com/collections/17978790-e8c27790-4bf8-455b-bb90-4b5c1c424c02?apikey=PMAK-656ac7d835341966d37ff0f9-c69b28b1075a764deb10584ea0b61b8e

Response : 







Running a collection from a file

Step1: Go the file location where collection is present 
Step2 :Run command 
 > newman run {collection_name}


















Specifying Environment With Newman (-e : environment flag )

https://api.postman.com/collections/{collection_id}api key -e https://api.postman.com/environments/{environment_id}

Generating HTML Reports



Link to download : 
https://www.npmjs.com/package/newman-reporter-htmlextra






> npm install -g newman-reporter-htmlextra
>newman run https://api.postman.com/collections/17978790-e8c27790-4bf8-455b-bb90-4b5c1c424c02?apikey=PMAK-656ac7d835341966d37ff0f9-c69b28b1075a764deb10584ea0b61b8e -r htmlextra

Report will get generated where you have downloaded newman



















Generating JUnit report

Junit : standard report format (XML)
it is inbuilt with newman so no need to install it 
>newman run https://api.postman.com/collections/17978790-e8c27790-4bf8-455b-bb90-4b5c1c424c02?apikey=PMAK-656ac7d835341966d37ff0f9-c69b28b1075a764deb10584ea0b61b8e --reporters junit
>  --reporter-junit-export <path>  (If you want to extract result to some other path )


Comments

Popular posts from this blog

Unified Payments Interface (UPI) : Detail Analysis

Postman Test Automation with Teamcity