Table of content

Assertions in POSTMAN

Assertions are nothing but code that can be used to check or analyze the response that is received from the server. In POSTMAN assertions is a javascript code that is executed after the response is received.

There are many ways to write the assertions in POSTMAN, one is the Snippets, which are nothing but a block of codes which has some unique functions inside it and are ready-made inside the POSTMAN software. Anyone can simply click into the snippets and get the code inside the test editor and run the test.

Here, we shall discuss assertions which are mainly performed on the responses which are in .json format.

What is JSON?

Before getting started with Assertions, it is very important to understand JSON and why it is important in POSTMAN.

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and also transporting data and comes into play when the data is received by any webpage from the server. JSON has gained its popularity because of its simplicity and self-describing nature.

Few things to note about JSON Syntax :

  • Data in JSON will be in name or value pairs.
  • Data will be separated by commas. (,)
  • Curly braces {} hold the objects.
  • And the arrays are written inside the []square brackets.
JSON Data

is always written as Name and Value. For example,

"first_name": "Rahul"

It indicated, that the name is first_name and the value is Rahul.

JSON Objects

JSON objects are written inside curly braces. An object in JSON can hold multiple Name-Value pairs. An object in JSON can be written as:

{"first_name" : "Rahul", "last_name" : "Kumar"}
JSON Arrays

JSON arrays are written inside square brackets:

"employees":[
    {"first_name":"Rahul", "last_name":"Kumar"},
    {"first_name":"Anya", "last_name":"Roy"},
    {"first_name":"Peter", "last_name":"Jones"}
]

The most common use of JSON is to read the data from the web-server and display the data on the desired webpage. In the JavaScript, there is an in-built function called-JSON.parse(text);. This method is used to convert the strings into a JavaScript object.

Assertions on Arrays

Assertion can be possible on different attributes such as an object, arrays, etc. In this section, we shall see how assertion can be written on JSON Arrays. As in POSTMAN, we have seen that the response received from the server is displayed in a JSON format by default in POSTMAN, of not, we can select JSON application and get the response converted into the JSON format.

Before we apply any assertions, we shall consider this Request:

https://reqres.in/api/users?page=2

And the response of this request is below:

{
    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 7,
            "email": "michael.lawson@reqres.in",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"
        },
        {
            "id": 8,
            "email": "lindsay.ferguson@reqres.in",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg"
        },
        {
            "id": 9,
            "email": "tobias.funke@reqres.in",
            "first_name": "Tobias",
            "last_name": "Funke",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"
        },
        {
            "id": 10,
            "email": "byron.fields@reqres.in",
            "first_name": "Byron",
            "last_name": "Fields",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"
        },
        {
            "id": 11,
            "email": "george.edwards@reqres.in",
            "first_name": "George",
            "last_name": "Edwards",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"
        },
        {
            "id": 12,
            "email": "rachel.howell@reqres.in",
            "first_name": "Rachel",
            "last_name": "Howell",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"
        }
    ]
}

To make use of JSON assertions, the first step is to parse the JSON body. Edit the below code into the editor:

let jsonData = pm.response.json();
console.log(jsonData.data);

This code pm.response.json() will parse the JSON body and the code console.log(jsonData.data) logs the object into the POSTMAN console.

postman-assertion-json-parse

Open the POSTMAN console, and see the arrays inside the object.

postman-assertion-console

As shown in the image, there are different objects listed numbered from 0 to 5, as there were 6 different objects. To access the name or value, the dot(.)operator is used.

Edit the below code to access the name inside the array:

let jsonData = pm.response.json();
console.log(jsonData.data[0].id);

postman-assertion-access-value

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now