4

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

Jhanz
  • 127
  • 3
  • 3
  • 12
  • 1
    What exactly is your problem? I would start with transforming the search query and the string you're testing to lowercase, so that the indexOf method works for both 'Pregnancy' and 'pregnancy'. Also, the string 'pregnancy' can be at index 0, so you should check if it is larger than -1. – Sjors May 17 '19 at 16:00

2 Answers2

4

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);
});
Woodrow
  • 2,740
  • 1
  • 14
  • 18
  • You're welcome. Please accept answer if it worked for you. Thank you! – Woodrow May 20 '19 at 12:52
  • Thanks for this answer, this has helped me as well.. question what if want a number count of the ones that have Pregnancy and a number count of those that don't have pregnancy .. because in my test i want to get the id of the ones that contain it as not all objects will contain it...thanks – shahbaz Oct 01 '20 at 15:51
1

Use the ES6 reduce function that accumulates the results of whether the name of each item includes "Pregnancy" while iterating through the response.

const jsonData = pm.response.json();
tests[`Each organisation name field contains \"Pregnancy\"`] = jsonData
  .reduce((acc, org) => acc && org.name.toLowerCase().includes("pregnancy"));
  • You'll find that ES6 functions like map and reduce are generally more expressive
  • Using inlcudes makes your intention clearer than indexOf
  • I added toLowerCase() so the matches are case insensitive

Passing postman test

Failing postman test