Renuka
Renuka

Reputation: 11

How to validate the response body in postman?

If I get this body only, I need the test case to be passed.

{
    "statuscode": "200",
    "res": [{
        "bwInfo": {
            "timeStamp": {
                "seconds": 0,
                "nanoSeconds": 0
            },
            "appIns_Id": "string",
            "requestType": "string",
            "sessionFilter": [{
                "sourceIP": "string",
                "sourcePort": [
                    "string"
                ],
                "destAddress": "string",
                "dstPort": [
                    "string"
                ],
                "protocol": "HTTPS"
            }],
            "fixedBWPriority": "1",
            "fixedAllocation": "100",
            "allocationDirection": "00"
        }
    }]
}

And the code that I have written for validation is:

 var jsonData = pm.response.json()
 if (jsonData !== null) {
     pm.test("isString", function() {
         for (i = 0; i < jsonData.length; i++) {
             pm.expect(jsonData[i]['bwInfo']['appIns_Id']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['fixedAllocation']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['allocationDirection']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['sourceIP']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['sourcePort']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['destAddress']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['dstPort']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['protocol']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['requestType']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['fixedBWPriority']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['timeStamp']).to.be.a('string');
         }
     });
 } else {
     pm.test("Not found", function() {

     });
 }

I want the test case to validate only if the status code is 200 but whatever status code is coming, it is still giving me a pass. Please guide me.

Upvotes: 1

Views: 6379

Answers (2)

Tahera Firdose
Tahera Firdose

Reputation: 181

You can validate any status code with the below snippet. Just modify the status code number in the script.

I have written in 2 different ways using pm.expect and pm.response. Use any one of them as per your convenience.

// To verify if the status code is  200
pm.test("Status code is 200", function () {

  // Using Response Assertion API
  pm.response.to.have.status(200);

  // Using explicit expect
  pm.expect(pm.response.code).to.eql(200);

});

Upvotes: 2

Veeresham Tammali
Veeresham Tammali

Reputation: 436

Hope below snippet will help you to check the response status code.

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

Source: http://blog.getpostman.com/2017/10/25/writing-tests-in-postman/

Upvotes: 2

Related Questions