Postman test to find a keyword in a response array

Jhanz picture Jhanz · May 17, 2019 · Viewed 7.2k times · Source

I am creating a test in postman to check for a keyword "pregnancy" in each "name" field in the JSON. If each 'name' field in the JSON contains the keyword then pass the test, else fail.

Find the script below that I have tried using

var jsonData = pm.response.json(); 

var resultCount = jsonData.length;
for (i=0;i<resultCount;i++){
    var modelString = jsonData[i].name;
    if(modelString.indexOf("Pregnancy") > 0)
    {
       tests["Each organisation name field " +i+ " contains   \"Pregnancy\""] = modelString.has("Pregnancy");
    }
    else
    {
       pm.expect.fail("failed");
    }
}
console.log("")

[
    {
        "id": "1-116992830",
        "name": "British Pregnancy Advisory Service      (BPAS)",
        "numberOfLocations": 78
    },
    {
        "id": "1-1560082724",
        "name": "PAMS Pregnancy Ultrasound Centre      (PAMS 3D &     4D Baby Imaging)",
        "numberOfLocations": 1
    },
    {
        "id": "1-2458518720",
        "name": "Pregnancy Ultrasound Ltd      (Babyface4d)",
        "numberOfLocations": 1
    },
    {
        "id": "1-101728376",
        "name": "National Unplanned Pregnancy Advisory Service     Limited",
        "numberOfLocations": 23
    },
    {
        "id": "1-3578030817",
        "name": "Private Pregnancy Ultrasound Services Ltd trading as Expectancy Scanning Studios Ltd      (Expectancy Scanning Studios Ltd)",
        "numberOfLocations": 2
    },
    {
        "id": "1-1412821832",
        "name": "Foundation For Life (Salisbury)      (Pregnancy Advice Salisbury)",
        "numberOfLocations": 1
    },
    {
        "id": "1-2028907839",
        "name": "Miscarriage Clinic Limited      (Centre for     Reproductive Immunolgy and Pregnancy)",
        "numberOfLocations": 1
    },
    {
        "id": "1-744810951",
        "name": "Foundation For Life      (Tyneside Pregnancy Advice Centre)",
        "numberOfLocations": 1
    }
]

What I'm expecting is, if 'pregnancy' is missing from the each array field, it should fail the test

Answer

Woodrow picture Woodrow · May 17, 2019

I added your JSON to https://api.myjson.com/bins/13qh7i to create an API to test with under Postman, and added one additional object in the array with a name that doesn't have the word "pregnancy" in it so you can see the test in action.

If you loop through and check the objects name property for the word "pregnancy", and store those that are found in the hasPregnancy array, you can then check the length of the array against the length of the API response object array to see if each object indeed contains the keyword "pregnancy".

Below is the JS code I used in the test. I also updated your indexOf to be includes since you should be looking for > -1 with indexOf. I also made the modelString variable apply toLowerCase() on the name to ensure checking doesn't care about capitalization, etc.

enter image description here

// Get response
var jsonData = pm.response.json();
var resultCount = jsonData.length;

// Test arrays
var hasPregnancy = [];
var doesntHavePregnancy = [];

// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
  var id = jsonData[i].id;
  var modelString = jsonData[i].name.toLowerCase();
  if (modelString.includes("pregnancy")) {
    hasPregnancy.push({
      "id": id,
      "hasPregnancy": modelString.has("pregnancy")
    });
  } else {
    doesntHavePregnancy.push({
      "id": id
    });
  }
}

// Check that each object in response contained keyword and length matches from test
pm.test("Expect response to contain pregnancy in each object", function() {
  console.log(hasPregnancy);
  console.log(doesntHavePregnancy);
  pm.expect(hasPregnancy.length).to.equal(resultCount);
});