2

I have a scenario to validate the "status" value in the array. The response is dynamic and # iteration may vary. I don't want to save this value in my postman environment but need to make a dynamic check. From my below API Response, I got 2 instances, 1st with AVAILABLE, 2nd with SOLDOUT. Can someone suggest to me how do I make the comparison?

Response API:

[
    {
        "status": "AVAILABLE",
        "price": {
            "baseAveragePrice": 209,
            "discountedAveragePrice": 209
        },
        "Fee": 39,
        "flag": false
    },
    {
        "status": "SOLDOUT",
        "price": {
            "baseAveragePrice": 209,
            "discountedAveragePrice": 209
        },
        "Fee": 39,
        "flag": true
    },
]

pm.test("status Check", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.status).to.be.oneOf(["AVAILABLE", "SOLDOUT", "NOTRELEASED"]);
});
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Sankar S
  • 19
  • 2
  • What do you mean by dynamic check? also, your response returning an array, hence `jsonData.status` won't give you actual status. You have to make it in a loop. – Divyang Desai May 06 '19 at 05:46

2 Answers2

1

If you're trying to check all of the status value in the response, you could iterate through them like this:

pm.test("status Check", function () {
    var jsonData = pm.response.json();
    _.each(jsonData, (arrItem) => {            
        pm.expect(arrItem.status).to.be.oneOf(["AVAILABLE", "SOLDOUT", "NOTRELEASED"]);
    })
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
1

Your snippet is actually working for one single element. Your current response is a JSON-array. So you need to iterate your check over the whole array.

One solution ist this:

pm.test("status Check", function() {
    var jsonData = pm.response.json();

    jsonData.forEach(function(arrayElement) {
        pm.expect(arrayElement.status).to.be.oneOf(["AVAILABLE", "SOLDOUT", "NOTRELEASED"]);
    });

});

This will return one single Test "status Check" with OK, if all of them are ok, and with FAILED if one of them fails.

one single test

If you want to see more details in your test-result, i would suggest to add each one of them in one nested test. With this solution you will have 3 Tests. One general Test "status Check" and one test for each Array item (in this case 2):

pm.test("status Check", function() {
    var jsonData = pm.response.json();

    jsonData.forEach(function(arrayElement) {
        pm.test("Status is either 'AVAILABLE','SOLDOUT' or 'NOTRELEASED'", function() {
            pm.expect(arrayElement.status).to.be.oneOf(["AVAILABLE", "SOLDOUT", "NOTRELEASED"]);
        });
    });

});

one test per array-item

DieGraueEminenz
  • 830
  • 2
  • 8
  • 18