Monday, March 29, 2021

POSTMAN-NewMan CLI API Automation

 INTRODUCTION

This article outlines automation of APIs using postman and new line command line interface as well as batch setup. The paper proposes a way to create collections in postman, use those collection in new man for automation testing and reporting the automation results in different formats.

Overview

Scope of testing will be API testing for this whitepaper. Postman-Newman CLI provides a command line interface for API automation to run quick API automation test

Use of Postman

• Testing rest services using POSTMAN

·         Workspace: stores all collections

·         Collections: collections of requests

·         Test suits : contains tests or test cases

• HTTP request Methods

·         Create a record- POST

·         Retrieve/verify data – GET

·         Update data – PUT

·         Delete data – DELETE

• URL : domain + URI (also call as endpoints)

API Architecture

API calls Collection include mainly three things:

• HTTP headers

• HTTP Request (POST, GET, PUT, DELETE )

• Status Code/ Response Code

·         Date

·         Version

·         Description

·         Author

Newman

Newman is a command line Collection Runner for Postman. It allows you to run and test a Postman Collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

Newman maintains feature parity with Postman and allows you to run collections the way they are executed inside the collection runner in Postman.

Newman resides in the NPM registry and on GitHub.

Pre-requisites

Installation can be done in two ways o Install as google plug in

·         Download as application

o   Download and install node js using below link Link: https://nodejs.org/en/download/

o   Install newman cli globally: npm install -g newman

o To check the version of npm : Npm -v

o To check the version of newman : Newman -v

To get HTML reports in postman ,downloa using the cmd : install -g newman-reporter-htmlextra

Usage

After installing, you can create request collections in postman and run the collection through postman

To share the collections through postman with other test members use below Share collection option.

 To export the collection use the Export button. Once you export the collection, use this collection to run through newman

 API Automation Framework in Postman-Newman CLI

API automation framework can we implement using following way.

A.      Define phase

·         In define phase- we need to create different test collection based on the test

·         requirement and set of data and environments

B.      Execute Phase

·         Once the collections are defined we need to export them and get check in the repositories using github or bitbucket

·         Write the batch job using command line interface for Newman commands to run the collection and generate the report formats

C.      Automate phase

·         In Jenkin, we need to create new item with freestyle project

·         Add build step with “Execute Shell” shell command

·         Assign the newman command or batch job in Jenkin project to get the continuous

·         integration

·         Click save

Run from command line with environment specific collection

Basic setup

--folder [folderName] Specify a single folder to run from a collection.

-e, --environment [file|URL] Specify a Postman environment as a JSON [file] -d, --iteration-data [file] Specify a data file to use either json or csv

-g, --globals [file] Specify a Postman global file as JSON [file]

-n, --iteration-count [number] Define the number of iterations to run

Request options

--delay-request [number] Specify a delay (in ms) between requests [number] --timeout-request [number] Specify a request timeout (in ms) for a request

Misc.

--bail Stops the runner when a test case fails

--silent Disable terminal output

--color off Disable colored output (auto

-k, --insecure Disable strict ssl

-x, --suppress-exit-code Continue running tests even after a failure, but exit with code=0 --ignore-redirectsDisable automatic following of 3XX responses

--verbose Show detailed information of collection run and each request sent

Use the -n option to set the number of iterations to run the collection. Steps to run the collection

o Go to command line with folder in which you kept the postman collection To run the test collection through Newman command line

  You can run collection on specific environment with below command newman run [collection_filename.son] -e [environment_filename. json]

Path of newman in system : C:\Users\[User Name]\newman

Save Results

Results can be saved as JSON data, HTML report, excel report, command line report, junit report and multiple format together as well. By passing a filepath to command you indicate where the file should be generated. If no filename is passed, a default will be used.

c:\Users\[user dir]\Desktop\Postman Files>newman run "example collection1.postman_collection.json" - d data.csv -r htmlextra

 -e is the flag for Environment. This should run through your entire Collection of end.

html report

Newman run “collection name.json” -e “environment.json” -r htmlextra

Junit report

Newman run “collection name.json” -e “environment.json” -r junit

excel report

Newman run “collection name.json” -e “environment.json” -r csv

Command line report

Newman run “collection name.json” -e “environment.json” -r cli

json report

Newman run “collection name.json” -e “environment.json” -r json

Multiple format report

Newman run “collection name.json” -e “environment.json” -r csv,htmlextra,junit

To export report to any location

Newman run “collection name.json” -e “environment.json” -r json -export “newpath” Result:

BDD Cucumber -resequencing the feature file and scenarios

 Hey Folks,

There may be a situation while you need to call your scenario outline of one feature needs to call in particular sequence based on application flow, this can be achieved by adding the tags to scenario outline with priority will work.

However, what happens when you need to sequence your scenario outline with different feature files? 

By default, BDD cucumber takes feature names alphabetically during the execution and then based on your tag sequences. So, if you want to resequencing your scenario in different features file, then you need to give your feature names in sequence of alphabet or append some numbering. if it still doesn't work then rename the feature file name itself and it will work. for example,

Feature file name : 01Feature1.feature, 02Feature2.feature

Feature Name: 01Feature1, 02 Feature2

Cheers,

TJ


API Testing -with POSTMAN

 Web services supported by POSTMAN:

Type of webservices

·         SOAP

·         REST

Installation of POSTMAN

·         Install as google plug in

·         Download as application

Use below URL for sample test rest API requests : https://reqres.in/

Tips:

·         JSON beautifier is used to check the data format

·         Testing rest services using POSTMAN

POSTMAN IDE:

·         Workspace: stores all collections

·         Collections: collections of requests

·         Test suits : contains tests or test cases

HTTP request Methods

·         Create a record- POST

·         Retrieve/verify data – GET

·         Update data – PUT

·         Delete data – DELETE

URL : domain + URI (also call as endpoints)

 Steps to write test cases

·         Get the request run and manually work

·         Understand/parse the request body

·         Write test

Working with get requests steps:

·         Take sample Get request with parameters

·         Go to tests section

·         Use the existing snippet as beginner

·         Write modern code use ES6 javascript code

·         Status code, json body value check

Working with post request steps

·         Let date=date.now() console.log(date)

·         Under pre-req set the prereq actions or steps

·         Set global variables (pm.globals.set(“currentdate”,date)

·         To use environment variable use double curly brackets in request body ex. “currentdate”:{{currentDate}}

 API Architecture

API calls Collection include mainly three things:

1.            HTTP headers

2.            HTTP Request  (POST,GET,PUT,DELETE )

3.            Status Code/ Response Code

Apart from this your application collection include many things if it required to test in you application.

1.       HTTP headers  - HTTP headers are always depended on your application, Mainly two:

·         Authorization - A token included with requests to identify the requester. This header has to be included in all requests other than the login request.

·         Content-Type - A standard MIME type describes the format of object data.Content –type in most of the requests and responses will be application/json.

 2.       HTTP Request  -  There are mainly four request, which we used frequently: DATA =

·         POST - Create Or Update data

·         PUT - Update data

·         GET - Retrieve data

·         DELETE – Delete data

3.       Status Code/Response Code - There are many status/response code, from them we can verify the response.

1.            200 - OK, The request was successful.

2.            201 - Created, The request was successful and data was created.

3.            204 - No Content, The response is empty.

4.            400 - Bad Request, The request could not be understood or was missing required parameters.

5.            401 – Unauthorized, Authentication failed or user does not have permissions for the requested operation.

6.            403 - Forbidden, Access denied.

7.            404 - Not Found, Data was not found.

8.            405 - Method Not Allowed, Requested method is not supported for the specified resource.

9.            500 - Internal Server Error.

10.        503 - Service Unavailable, The service is temporary unavailable.

Postman tips

·         Postman console for debugging- if any error occurred during test snippets, you can verify it using postman console window

·         Postman code generator- below save button, there is link as Code

·         To generate delay between requests- on request > Test tab – set Timeout(()=>{},10000) (10000 means 10 sec)

·         To skip the test in postman request- Go to Test and lets say you have 5 validation out of which need to skip one, you need to write pm.test.skip(“Testname”,function(){pm.expect(response.origin).to.eql(100);});

·         Conditional skip – const islocal=pm.environment.get(‘environment’)==’local’; (islocal?pm.test.skip: pm.test(“Testname”,function(){pm.expect(response.origin).to.eql(100);});

Cheers,

TJ