2

The response body looks like this.


[
    {
        "id": 1,
        "wardName": "Unit1"
    },
    {
        "id": 2,
        "wardName": "Unit2"
    },
    {
        "id": 3,
        "wardName": "Unit3"
    },
    {
        "id": 4,
        "wardName": "Unit4"
    }
]

Here, I need to verify whether the ward names (Unit1, Unit2, Unit3, and Unit4) have correct values.

2

You can create a Postman test:

pm.test("Checking wardNames", () => {
  const responseJson = pm.response.json();
  pm.expect(responseJson[0].wardName).to.eql(Unit1);
  pm.expect(responseJson[1].wardName).to.eql(Unit2);
  pm.expect(responseJson[2].wardName).to.eql(Unit3);
  pm.expect(responseJson[3].wardName).to.eql(Unit4);
});

Of course, you can code these assertions differently as you want.

4
  • Hi Joāo, Thank you for the answer. I have tried with that but gives the following error. "Checking wardNames | TypeError: Cannot read property '0' of undefined" Oct 25 at 7:29
  • 2
    @kasunigunasekera: there should be no list, the array from the response is already accessible through responseBody, so pm.expect(responseJson[0].wardName).to.eql('Unit1'); is how to write these checks. Also eql() has to take a string, Unit1 without quotes will try to find a variable with such a name. More on that in Postman docs learning.postman.com/docs/writing-scripts/script-references/…
    – pavelsaman
    Oct 25 at 8:22
  • @pavelsaman That worked! Thanks a lot, both. Oct 25 at 8:31
  • 1
    Yeah, I assumed there would be some object of type list, not a list on the root of a JSON object - this design is odd. Oct 25 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.