Reputation: 19
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"]);
});
Upvotes: 2
Views: 2443
Reputation: 880
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.
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"]);
});
});
});
Upvotes: 1
Reputation: 25881
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"]);
})
});
Upvotes: 1