Compare 2 responses using Postman

Hi Team,

I have scenario where i need to compare 2 responses from Rest API with different test data.
Can i execute same Rest API in a loop in postman where it pickup test data 1 in 1st iteration and test data 2 in second iteration?

Post that I need to compare responses(response are in JSON format) for 2 iterations.

Thanks.

I think you can set a counter variable using for mark 2 iteration, use this counter, set 2 different variables to record 2 responses,then compare them.

I don’t know if it will help you, if not, I’m sorry,I‘’m a novice.

@SatejMirpagar - Have you tried using the collection runner in the postman app?
You can configure it with iterations count with which you want to run a collection / folder.

You can set a global / environment variable for the purpose of keeping the iteration count.
You can set the counter to 1 before starting the runner, as a global variable.

You can have the following simple test script for the request

let c = parseInt(pm.globals.get('counter'));
if (c == 1) {
    pm.test('should equal 1st iteration data', function() {
        // check here
    });
} else if (c == 2) {
    pm.test('should equal 1st iteration data', function() {
        // check here
    });
}
pm.globals.set(counter, c + 1);

pm.test('Check if the response is a JSON', function() {
    // check here
});

Hope this helps.

The approach above is valid and extendible to an arbitrary number of iterations, but to compare only two responses, I would go with a simpler, more explicit approach:

  1. Make two requests in Postman, lets call them A and B
  2. As part of the post-request/tests script of request A, register the response as a global variable using the following snippet
pm.globals.set("aResponseAsJson", pm.response.json());
  1. As part of the post-request/tests script of request B, you now have access to response A in a global var and the response from the current request. You can compare them as needed using ChaiJS BDD style. Finish by cleaning up the global variable.
pm.test("compare responses", () =>
    pm.expect(pm.response.json()).to.equal(pm.globals.get("aResponseAsJson")));
pm.globals.unset("aResponseAsJson");
1 Like

Thanks mates - the information really helped!

Would you want to use to.deep.equal() here incase the response has nested properties?

Hi!

I’ve been struggling with this issue in the past week and wrote the following test script:

const responseKey = [pm.info.requestName, 'response'].join('/');

let res = '';

try {
    res = JSON.stringify(pm.response.json());
} catch(e) {
    res = pm.response.text();
}

if (!pm.globals.has(responseKey)) {
    pm.globals.set(responseKey, res);
} else {
    // this tests the actual response with one saved in the global for the same request
    pm.test(responseKey, function () {
        const response = pm.globals.get(responseKey);
        pm.globals.unset(responseKey);
        try {
            const data = pm.response.json();
            pm.expect(JSON.stringify(data)).to.eql(response);
        } catch(e) {
            const data = pm.response.text();
            pm.expect(data).to.eql(response);
        }
    });
}

You this code in a collection or single request.

Hope it helps.

I am getting the below error when I added the above script. note that the API request is the same.
I added this schema in both requests :

Storeschema = {
   "properties":{
      "errors":{
         "type":"boolean"
      },
      "data":{
         "type":"object",
         "properties":{
            "item_favorites":{
               "type":"array",
               "items":[
                  {
                     "type":"object",
                     "properties":{
                        "id":{
                           "type":"number"
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

and im getting

compare responses | AssertionError: expected { Object (errors, data) } to equal { Object (data, errors) }

Hey @Nisrine,

Could you update your comment to add in the response body that’s returned and also the test script doing the validation please?

I will help explain the issue you’re facing a little better. :slight_smile:

First you have to set the environment which is having 2 base url's one is of STG OR UAT and other is PROD
==========================================================================
{{API_UAT}}/app/api.json // this is request 1

// then in test request Script of UAT/STG
================================================================
var jsonData = pm.response.json();
let y = ;

pm.test("Status code is 200", function() {
    pm.response.to.have.status(200);
    statusCode = true;
});
pm.test("Response time is less than 1000ms", function() {
    pm.expect(pm.response.responseTime).to.be.below(1000);
});


if (statusCode === true) {
    const jsonData = pm.response.json();
    const ErrorSchema400 = {
        "type": "object",
        "properties": {
            "assets": {
                "type": "null"
            },
            "status": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer"
                    },
                    "message": {
                        "type": "string"
                    }
                },
                "required": [
                    "code",
                    "message"
                ]
            }
        },
        "required": [
            "assets",
            "status"
        ]
    };
    const ErrorSchema404 = {
        "type": "object",
        "properties": {
            "assets": {
                "type": "null"
            },
            "status": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer"
                    },
                    "message": {
                        "type": "string"
                    }
                },
                "required": [
                    "code",
                    "message"
                ]
            }
        },
        "required": [
            "assets",
            "status"
        ]
    };
    if (jsonData.status.code == 200) {
        pm.test('Json is valid', () => {
            var ignoreDigit = new RegExp("^[0-9]*$");
            var keys = extractJSON(jsonData, []);

            function extractJSON(obj, keys) {
                for (const i in obj) {
                    if (/^"0-9"/.test(i)) {
                        continue}
                    if (Array.isArray(obj[i]) || typeof obj[i] === 'object') {
                        if (!ignoreDigit.test(i))
                            y.push(i);
                        //console.log("Outer", i);
                        //keysn = i;
                        extractJSON(obj[i], keys + ' > ' + i + ' > ');
                    } else {
                        if (!ignoreDigit.test(i))
                            y.push(i);
                        //console.log("Nested", i);
                    }
                }
            }
            var keysArr = y;
            //console.log(keysArr);
            pm.test("Keys of UAT API are " + " = " + keysArr.sort());
            pm.environment.set("ALL UAT KEYS", keysArr.sort());
        });

    } else if (jsonData.status.code == 400) {
        pm.test('code: 400,Refer some Error in response', () => {
            const schema = ErrorSchema400; // Only checking for success response at present
            pm.expect(tv4.validate(jsonData, schema)).to.be.true; {
                throw new Error("Please refer Validation in Response");
            } //to treat as an error 
        });
    } else if (jsonData.status.code == 404) {
        pm.test('code: 404,message:Data not Found', () => {
            const schema = ErrorSchema404; // Only checking for success response at present
            pm.expect(tv4.validate(jsonData, schema)).to.be.true;
        });
    }
}

Then in production response
{{API_PROD}}/app/api.json // this is request 2 production API

Then in test request of prod Api
NOTE:- Please make sure that use the same collection and environment but base url will be different
==============================================================================
var jsonData = pm.response.json();
let x = ;
var NewArray1 = ;
var NewArray2 = ;

    pm.test("Status code is 200", function() {
        pm.response.to.have.status(200);
        statusCode = true;
    });
    pm.test("Response time is less than 1000ms", function() {
        pm.expect(pm.response.responseTime).to.be.below(1000);
    });


    if (statusCode === true) {  // this is the real schema validation 
        const jsonData = pm.response.json();

        const ErrorSchema400 = {
            "type": "object",
            "properties": {
                "assets": {
                    "type": "null"
                },
                "status": {
                    "type": "object",
                    "properties": {
                        "code": {
                            "type": "integer"
                        },
                        "message": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "code",
                        "message"
                    ]
                }
            },
            "required": [
                "assets",
                "status"
            ]
        };
        const ErrorSchema404 = {
            "type": "object",
            "properties": {
                "assets": {
                    "type": "null"
                },
                "status": {
                    "type": "object",
                    "properties": {
                        "code": {
                            "type": "integer"
                        },
                        "message": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "code",
                        "message"
                    ]
                }
            },
            "required": [
                "assets",
                "status"
            ]
        };
        if (jsonData.status.code == 200) {
            pm.test('Json is valid', () => {
    //code to fetch all json keys whether nested or other no need to go for object.keys !!
                var ignoreDigit = new RegExp("^[0-9]*$");
                var keys = extractJSON(jsonData, []);

                function extractJSON(obj, keys) {
                    for (const i in obj) {
                        if (/^"0-9"/.test(i)) {
                            continue}
                        if (Array.isArray(obj[i]) || typeof obj[i] === 'object') {
                            if (!ignoreDigit.test(i))
                                x.push(i);
                            //console.log("Outer", i);
                            extractJSON(obj[i], keys + ' > ' + i + ' > ');
                        } else {
                            if (!ignoreDigit.test(i))
                                x.push(i);
                            //console.log("Nested", i);
                        }
                    }
                }
                var keysArr = x;
                //console.log(keysArr);
                pm.test("Keys of PROD API are" + " = " + keysArr.sort());
                pm.environment.set("ALL PROD KEYS", keysArr.sort());


                pm.test("[>>PASS(If  PROD and UAT Keys are equal)]== OR ==>[>>FAIL(If any Mismatches => Refer details in Report[Total Requests]== OR ==>[>>FAIL(No Mismatches but Shortage of Keys Array]", function() {
                    pm.expect(pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}')).to.deep.equal(pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}'));
                });//this is the main test case which is comparing the two different Environment json keys

                function diff(array1, array2) {  //  Here User can compare the 2 different Api json keys to me matched 
                    var arra1 = pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}');
                    //console.log("UATUATUATUAT", pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}'));
                    var arra2 = pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}');
                    //console.log("PRODPRODPROD", pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}'));
                    arra1 = arra1.toString().split(",").map(String);
                    arra2 = arra2.toString().split(",").map(String);

                    for (var i in arra1) {
                        if (arra2.indexOf(arra1[i]) === -1) NewArray1.push(arra1[i]);
                    }
                    for (i in arra2) {
                        if (arra1.indexOf(arra2[i]) === -1) NewArray2.push(arra2[i]);
                    }

                    pm.test("Keys present in UAT api but not in PROD api = " + NewArray1.sort((a, b) => a - b));
                    pm.test("Keys present in PROD api but not in UAT api = " + NewArray2.sort((a, b) => b - a));
                //console.log("Keys present in UAT api but not in PROD api = " + NewArray1.sort((a, b) => a - b));
                //console.log("Keys present in PROD api but not in UAT api = " + NewArray2.sort((a, b) => b - a));
                    var comparison = [];
                    comparison[0] = NewArray1.sort((a, b) => a - b);
                    comparison[1] = NewArray2.sort((a, b) => a - b);
                    return comparison;
                }
                diff("arra1", "arra2");
            });

        } else if (jsonData.status.code == 400) {
            pm.test('code: 400,Refer some Error in response', () => {
                const schema = ErrorSchema400; // Only checking for success response at present
                pm.expect(tv4.validate(jsonData, schema)).to.be.true; {
                    throw new Error("Please refer Validation in Response");
                } //to treat as an error 
            });
        } else if (jsonData.status.code == 404) {
            pm.test('code: 404,message:Data not Found', () => {
                const schema = ErrorSchema404; // Only checking for success response at present
                pm.expect(tv4.validate(jsonData, schema)).to.be.true;
            });
        }
    }
//===========JAI SHREE MAHANKAL========OM BAM SHIV=================