Basics of #Postman | Rajesh Kunapareddy | Senior SDET

Postman journey

Basics of #Postman | Rajesh Kunapareddy | Senior SDET

Introduction

Like all aspiring SDET's/QA's, I need to run daily tests on API's which were the first line of defense of testing an application before env is set up with all other dependencies packed and built into a fully functional environment.

Unlike many other documentations you might have gone through, I'm not planning to give fundamentals for any tools, the moto of this documentation is to write about what did I do to get over the basic roadblocks I faced on my initial journey of API automation as a senior SDET.

How did I decide to go with postman instead of rest-assured or any other tools?

Initial idea is to integrate the API suite into a build where it should run as frequently as a code commit. Including tools like maven, rest-assured, JAVA can complicate things and will eat away time for development and integration.

What does postman offer that other tools don't?

  • Postman is pretty flexible. One can right away start with hitting endpoint and assert response with pre-defined assertions or very minimal JS code.
  • Integrating has been simplified by the Newman plugin. Just export your Postman collection and run from a command line.
  • Reporting can be done by plugins. No code is needed.

Some quick code snippets I use

Manier times I needed some unique identifier so that it won't cause duplicate data.

There is two way I do this depending on the needs. When I know that I need to run a loop, simply use the postman iteration variable which will start from 0 when you run collection.

pm.variables.set("counter", pm.info.iteration);

Or create a random string using JS

var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charactersLength = characters.length;
for ( var i = 0; i <= 4; i++ ) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

First thing is to save the entire response into a variable to play with later.

Postman makes it easy by letting us copy response JSON into a variable using below JS code

const jsonData = pm.response.json();

Once data is saved into the "jsonData" variable, there are

Multiple ways of extracting data from it

For starters, very basic is to extract auth token from authentication request and save for further use

Considering your response got object "token", one can retrieve it just by saying

jsonData.token will give object token's value from the response, as we already got reponse saved into the "jsonData" variable.

Further, this needed to be saved to a Postman variable for future use.

postman.setEnvironmentVariable("token", jsonData.token);

Extract data from a complex response payload

As our JSON response has already been saved into a variable, now we can write JS script on top of the variable to extract required data.

var jsonData = pm.response.json();
pm.test("Extract data", function () {
    for (var i = 0; i < jsonData.length; i++)
    {
        if (jsonData[i].name == "India")
        {
            pm.environment.set("countryID", jsonData[i].id);
            pm.environment.set("countryLanguageID", jsonData[i].languages[0].languageId)
        }
    }
});

Where jsonData[i] will have info of country, then objects in that can be extracted by adding jsonData[i].

Run a simple test of HTTP status of 200

Postman tests will be run post API responds with a response payload and a HTTP status code. Response payload includes the resource requested by API and the status code gives the status of API execution at the backend. Let's say API is successfully executed at the backend then it will be a "200" status code. For any server-side failures, it will be 500 series response codes and there are many other status codes that will be no harm to learn.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Postman uses ChaiJS as a language to write tests that support BDD(Behaviour Driven Development) where we can write tests using keywords like "to", "be", "been", "is", "have" etc.

How to Integrate automated script into the daily job

Postman collections can be exported to be saved as JSON files which can be run via command line using an npm package "Newman". In the build job, once the env deployment is done test run can be done by using the Newman command as below,

newman run "yourCollectionFile_collection.json" --env-var "host=<stageURL>" --reporters html --reporter-html-export "Output\Reports\Test_Report.html"

Where "--reporters html --reporter-html-export" can create a report with necessary details of test exectution.

image.png