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: