POSTMAN jetpacks TESTING within for loop

15,278

Solution 1

I could make test with for loop. My Json:

{
  "rows": [
    {
      "id": "2804",
      "title": "Some title",
      ...
    },
    ...
  ],
  "total": "2788"
}

My test:

for (var i in data.rows){
    var obj = data.rows[i];
    tests["title of row #" + i + " is not null"] = !!obj.title;
    tests["title of row #" + i + " is not empty"] = obj.title !== "";
}

But if I use "return true" Postman shows "Tests (0/0)"

Solution 2

You can make use of forEach loop for iterating the result set returned in response. It returns the objects of specified type which can be used for processing.

var data = JSON.parse(responseBody); 
data.forEach(function(process){
    var processId = "Id" + process.id;
    //console.log("processId" + processId);
}) 
Share:
15,278
Ranson Namba
Author by

Ranson Namba

Updated on June 07, 2022

Comments

  • Ranson Namba
    Ranson Namba almost 2 years

    Hitting API w/ GET request, checking if item has been deleted by globals.id variable, have test inside forloop and when I run test returns 0/0 tests passed. All of my console logs within the for loop work, the objects contain values matching what I have as well. Anyone know how to do this?

    var data = JSON.parse(responseBody);
    
    
    for (var i = 0; i < data.length; i++){
      tests["id has been deleted"] = data[i].id !== globals.id;
      if(data[i].id !== globalID){
        tests["id has been deleted"] = data[i].id !== globals.id;
        return true;
      }
    }
    
  • Pascal Soucy
    Pascal Soucy over 7 years
    not an answer to the question - please comment in the comment section
  • sam
    sam over 6 years
    is there any way we can exit loop when some condition is true. Like we use break to exit from loop.