Testing Properties and Arrays correctly in POST

Hi All,

I need some help to create some test cases where my url is fetching data in such form:

I am using POST method.

Please help!!

Hi @ks.suhas12996, so adding assertions, to the API responses is depending upon the requirements. Basically we will be validating the response codes, schema and some assertions for the values in the response to check if they match the expected.

var jsonData = pm.response.json();

console.log(jsonData);

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

pm.test("resp include all fields", function () {
    pm.expect(jsonData).to.have.all.keys('totalAmount', 'batchSize', 'results');
});

I have just added some basic tests. Please let me know your expectations on the Tests further. Also I suggest you to watch the below video:

1 Like

Thanks @bpricilla If suppose i wanted validate paymentCode, terminalLanguage, terminalSerialNumber, guid and locationName parameters then how i can write the assertion? please help

What do you want to validate? That each record has specific values? Or that each record just has those properties?

@allenheltondev need to validate whether the parameters values are present in it or not!

There are two ways you can do it:

  1. Quick, easy way specifically looking for those properties
  2. The “right” way that scales and grows with your API

The quick way is to just iterate through the results and check for the properties:

const jsonData = pm.response.json();
_.foreach(jsonData.results, function(result) {
  pm.test('Result has expected properties', function(){
    pm.expect(result).to.have.property('paymentCode');
    pm.expect(result).to.have.property('terminalLanguage');
    pm.expect(result).to.have.property('terminalSerialNumber');
    pm.expect(result).to.have.property('guid');
    pm.expect(result).to.have.property('locationName');
  });
});

The more correct/scalable way would be to create a json schema and validate the entire response against it using ajv. There are even tools available online for free where you could take the response message you shared here and turn it directly into a json schema.

@allenheltondev I need to validate the Parameter values in response. Suppose if i want to validate merchantId parameter then how we can write a test? No need to validate merchantId value = borgun but need to validate whether any values are present in merchantId or not . Please help on this

@orbital-module-astr1 may be you can try this:

const jsonData = pm.response.json();
  pm.test('Not null', function(){
    pm.expect(jsonData.results.merchantName).not.equal(null);
    pm.expect(jsonData.results.merchantId).not.equal(null);
      });
1 Like

@ks.suhas12996 and @orbital-module-astr1 Also I suggest you to have a look at the Chai Library, this will be useful for writing assertions.

https://www.chaijs.com/guide/styles/

thank you so much!! @bpricilla . If i want to validate the action parameter from referenceObject from above response how i can put the assertion? because its a nested object feeling difficult. SO can you pls help me on this

With nested objects you just need to keep adding dots.
result.referenceObject.action

I would highly recommend learning a little about how to use json with javascript if you’re going to be using Postman to any degree.

In your response object, results is an array (you can tell because the contents are surrounded by square brackets [ ]) so you can’t just use dot notation to test the fields. You will have to iterate over them like I suggested in my response above, or you will have to access them by specific index.

That is certainly my mistake. Programming is case sensitive, and I made a typo.

instead of _.foreach you need to do _.forEach with an uppercase E

1 Like

thanks @allenheltondev its working fine now. And also i have one query like how can we test both conditions not being null and not being undefined? can you please help me in this?

just add .and.to.not.equal(null).and.not.be.undefined; to the end.

const jsonData = pm.response.json();
_.foreach(jsonData.results, function(result) {
  pm.test('Result has expected properties', function(){
    pm.expect(result).to.have.property('paymentCode').and.to.not.equal(null).and.not.be.undefined;
    pm.expect(result).to.have.property('terminalLanguage').and.to.not.equal(null).and.not.be.undefined;
    pm.expect(result).to.have.property('terminalSerialNumber').and.to.not.equal(null).and.not.be.undefined;
    pm.expect(result).to.have.property('guid').and.to.not.equal(null).and.not.be.undefined;
    pm.expect(result).to.have.property('locationName').and.to.not.equal(null).and.not.be.undefined;
  });
});

I’d highly recommend reading the link that @bpricilla shared above about chai.