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:

{

"totalAmount": 1,

"batchSize": 1,

"results": [

    {

        "amount": "37.00000",

        "issuerRequestStan": "000028",

        "issuerForwardingInstitutionIdCode": "352010",

        "deviceType": "A920",

        "acquirerTid": "00010069",

        "paymentCode": "C",

        "issuerResponseOriginalDataElements": "000028",

        "isSingleEmvMessage": false,

        "terminalDateTime": "20200930144244",

        "currCode": "ISK",

        "protocolLookupName": "BorgunProtocol",

        "issuerRequestOriginalDataElement": "010000002809301436580000000148300000352010",

        "issuerRequestMessageReasonCode": "4352",

        "merchantName": "Borgun",

        "issuerRequestProcessingCode": "000000",

        "issuerResponseCode": "00",

        "issuerAcquirerInstitutionIdCode": "001483",

        "country": "Iceland",

        "maskedCardNumber": "360705****1202",

        "partnerId": "test__handpoint",

        "routeName": "borgun-primary",

        "issuerRequestTimestamp": "20000930143658",

        "@version": "1",

        "terminalLanguage": "en",

        "terminalSerialNumber": "0821298878",

        "guid": "669b36d0-032a-11eb-9275-f907cabca0da",

        "locationCountryA2": "IS",

        "locationZipCode": "108",

        "name": "Sale Reversal",

        "agreementNumber": "9087059",

        "locationCity": "Reykjavik",

        "responseText": "APPROVED",

        "protocolName": "BORGUN",

        "cardTypeId": "Diners",

        "issuerRequestAcquirerTid": "00010069",

        "posDataCode": "81010100034C",

        "action": "REVERSAL_REQUEST",

        "serverDateTime": "20200930143700",

        "type": "Reversal",

        "locationStreet": "Ármúli 30",

        "merchantId": "borgun",

        "locationName": "Ármúli 30",

        "transNum": "000093000028",

        "status": "200",

        "@timestamp": "2020-12-08T14:40:24.558Z",

        "issuerResponseTimestamp": "20200930143702",

        "referenceObject": {

            "guid": "64e60cc0-032a-11eb-9275-f907cabca0da",

            "action": "AUTHORIZATION_REQUEST",

            "status": "200"

        },

        "timeZone": "CUT",

        "mcccode": "5999"

    }

]

}

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?

@allen.helton 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.

@allen.helton 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.

HI @allen.helton tried with your method but still am not able to get the response. am getting for each is not function as error message

Response:

{

"totalAmount": 1,

"batchSize": 1,

"results": [

    {

        "amount": "37.00000",

        "issuerRequestStan": "000028",

        "issuerForwardingInstitutionIdCode": "352010",

        "deviceType": "A920",

        "acquirerTid": "00010069",

        "paymentCode": "C",

        "issuerResponseOriginalDataElements": "000028",

        "isSingleEmvMessage": false,

        "terminalDateTime": "20200930144244",

        "currCode": "ISK",

        "protocolLookupName": "BorgunProtocol",

        "issuerRequestOriginalDataElement": "010000002809301436580000000148300000352010",

        "issuerRequestMessageReasonCode": "4352",

        "merchantName": "Borgun",

        "issuerRequestProcessingCode": "000000",

        "issuerResponseCode": "00",

        "issuerAcquirerInstitutionIdCode": "001483",

        "country": "Iceland",

        "maskedCardNumber": "360705****1202",

        "partnerId": "test__handpoint",

        "routeName": "borgun-primary",

        "issuerRequestTimestamp": "20000930143658",

        "@version": "1",

        "terminalLanguage": "en",

        "terminalSerialNumber": "0821298878",

        "guid": "669b36d0-032a-11eb-9275-f907cabca0da",

        "locationCountryA2": "IS",

        "locationZipCode": "108",

        "name": "Sale Reversal",

        "agreementNumber": "9087059",

        "locationCity": "Reykjavik",

        "responseText": "APPROVED",

        "protocolName": "BORGUN",

        "cardTypeId": "Diners",

        "issuerRequestAcquirerTid": "00010069",

        "posDataCode": "81010100034C",

        "action": "REVERSAL_REQUEST",

        "serverDateTime": "20200930143700",

        "type": "Reversal",

        "locationStreet": "Ármúli 30",

        "merchantId": "borgun",

        "locationName": "Ármúli 30",

        "transNum": "000093000028",

        "status": "200",

        "@timestamp": "2020-12-08T14:40:24.558Z",

        "issuerResponseTimestamp": "20200930143702",

        "referenceObject": {

            "guid": "64e60cc0-032a-11eb-9275-f907cabca0da",

            "action": "AUTHORIZATION_REQUEST",

            "status": "200"

        },

        "timeZone": "CUT",

        "mcccode": "5999"

    }

]

}

Please let me know how i can validate action parameter for referenceObject method.

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 @allen.helton 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.