Command-line integration of Newman CLI with Jenkins and Slack

Abhilash

Sep 28, 2020 | 6 min read

Earlier we have seen how to run postman collection in the postman.
This blog covers how to run a Postman collection in cli and send its reports to the slack

What is Newman?
It is command-line collection runner for Postman and also knows as Postman cli.

Why we need Postman cli?
Generally, the common purpose of the postman cli is to automate the task which we used to run on a daily basis For E.g Sending a report to your dev, Sanity test suite after the release.

How to Install Newman?
You install Newman using npm that is node package manager [Prerequisite you need to install node in your system and you can all the details from this link:- official Node.js site]

After installing Nodejs and npm, you can run the below command.

npm install -g newman

Or you can Follow this link Offical Postman site

Running the Tests in local


Once Newman setup is completed, we would have a sample postman collection which we are going to run in the newman

We are going to use Postman Echo sample Request.And you get following collections from this Link

Lets run the test for the collection which . For running the newman locally we need to use below command

newman run PostmanEcho.postman_collection.json
 -e PostmanEnvironment.postman_environment.json

Running above command will generate below output

Integrate Newman with Jenkins

Requirement:- Run a postman collection daily at certain time
Creating a Freestyle Job as I want to run periodically at a particular time.

Create a freestyle project in Jenkins

Adding Repository for the project we have to run.

Now we need to run our Jenkins job every day at 8:00 am [or Whatever time you want to choose]
For achieving that facility we need to add a cron job In Jenkins, there is default option named Poll SCM

In the help section, you can get to know what is Poll SCM,
In simple words, we can say it is a job scheduler, we can ask it to run the job at a particular time.
Currently, I have set H 8 * * *

It means that this job will run at 8 clocks daily
If you want to check the Cron written is correct or not, then you can use this Website.
Now hit the save button for saving the job which we made.

Uploading Postman Reports to S3


We have completed 2 parts till now i.e Running Newman test in local and  integrating it with Jenkins

Let’s move 1 Step further with uploading the generated reports to the aws S3

Before achieving that we need a few things to set up.

Prerequisites

We need an AWS account with an access token and secret token and S3 bucket for storage.
Assuming everybody has an AWS account. Then we  need to set up the aws CLI

You need to Install AWS Cli into the system.

For Windows, Linux and Mac installing the aws cli are different ways. So I am sharing the link to follow and do it as per your OS

For Windows:- https://docs.aws.amazon.com/cli/latest/userguide/install-windows.html

For Linux:- https://docs.aws.amazon.com/cli/latest/userguide/install-linux.html

For macOS:- https://docs.aws.amazon.com/cli/latest/userguide/install-macos.html

Once You have installed AWS cli in your system, you can configure aws cli with your access token, secret token, region and output

How to obtain Access and Secret key for AWS cli.

Login into aws account and click on your name which will be top Right-side corner

  • And select  My Security Credentials
  • Click on Create Access Key
  • On Creating New Access Key you will get tokens i.e Access and Secret

So now you have both access key and as well as a secret key and also download the CSV file because we are going need that details further.

Now In your terminal or command prompt type this command

aws configure

And add access key and secret key and keep rest to defaults.

AWS Access Key ID [****************P5WR]:
AWS Secret Access Key [****************1nM4]:
Default region name [us-east-1]:
Default output format [None]:

Let us check whether the file is being uploaded or not by our aws configurations  
Take the below command and send any file to upload in the bucket and check-in s3 bucket whether the file is uploaded or not.

aws s3 cp [filename.txt] s3://bucketname

Suppose the file is uploaded successfully then you will able to see that file in the s3 bucket. So we are moving to the final part that is Sending report to slack.

Let us create an app for a slack

In the above, we learnt how to run a Newman in local and setup cron job for same using Jenkins and

Navigate:- https://api.slack.com/apps

After Navigating to the above URL, you can  something on the screen as below and would be asking for Creating an app

Let us give a name to the App and for which Workspace it belongs to.

After the app is created you will get the following screen where you can activate the Incoming webhooks. Click on the add new Webhook to the workspace

Here you can say the app for which Slack-Channel can you integrate the app.
After creating the Webhook. Copy the URL and put somewhere you can access easily.

There will be a message in the channel in which the app is integrated


You an Confirm that App is created successfully and webhook is also integrated

Here, the Slack Webhook Integrations is Finished.

Final Step


Combining everything we did earlier

  • Jenkins Job
  • AWS access code and Secret code
  • Slack webhook

Now we are going to Run

you can clone the project to send notifications in the slack.
https://github.com/abhilashshettigar/NewmanToSlack.git
And also assuming that we are very pretty much aware of how to download the collections and environments
Before cloning the project you need to slack webhook and aws access key and secret key and S3 bucket. So we have got all the details which we need to run the project.
After cloning the Project, Create a new file name .env or follow this command

cp .env.example .env


It will create a new file containing everything  from .env.example to .env, Add the values which we obtain in the above steps in  the .env file

SLACK_WEBHOOK_KEY="https://hooks.slack.com/services/Tas3BaN/BQHLM5ZSM/VZq0lmKQTHgU4ReG9bGtZBssh2"
AWS_ACESS_KEY=XXXXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
BUCKET_NAME=newmanreports

After filling up the .env values it would look pretty much the same


Primary Changes to make in the Scripts

newman.run({
  collection: require('./tests/PostmanEcho.postman_collection.json'),
  environment: require('./tests/PostmanEnvironment.postman_environment.json'),
  reporters:  ['cli', 'htmlextra'],
  reporter: {
    htmlextra: {
        export: exportPath,
        template: './tests/templates/dashboardTemplate.hbs'
    }
  }

Here in the above snippet [Inside:- reportToSlack.js]
We need to Add your Postman collection inside the test folder and same for Environment.

Htmlextra gives an extra parameter that is Export and Template. Export is used to save the report in the given directory and template is used to show in the different Reports

You can get details for this package over HERE or you can install it using below command

npm install -g newman-reporter-htmlextra

Okay once you have added your collections path as shown above. And followed all the steps then we can proceed to run the scripts to get the slack notifications.

To run project we need to install all the dependencies first so running the below command

npm install

Once Installing dependencies completes. We can run the following command to run the project.

npm run test

All the things are set now running above command will generate a report , Uploads to S3 bucket and sends a notification to the Slack.

And slack notifications with S3 URL where we have uploaded the reportsin the following manner[Note:- Before running this we have to create s3 bucket]