2

How to verify in postman regardless of the number of results that all of data in the response returns id, firstname, lastname etc

Here is what response look like:

[
    {
        "id": 1,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 4
    },
    {
        "id": 5,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 8
    },
    {
        "id": 9,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 12
    }
]

I want to verify two things:

1) The response returns id, first_name, last_name, email

2) All the first_name is equal to "Sebastian" regardless there is only one result or 100

This is what i tried however, it only works for one result:

const jsonData = pm.response.json();

pm.test('Has data', function() {
  pm.expect(jsonData).to.have.property('first_name');
  pm.expect(jsonData).to.have.property('last_name');
  pm.expect(jsonData).to.have.property('email');
  pm.expect(jsonData).to.have.property('id');

});
Poonam
  • 166
  • 2
  • 14
  • Have you tried checking if the returned JSON is a JSON object or a JSON array? If it is an array they you will probably need to pust your has data check inside of a forEach/for loop. – Jim Factor Oct 26 '18 at 09:27

1 Answers1

1

You could try this:

pm.test("Has data",() => {
    _.each(pm.response.json(), (item) => {
      pm.expect(item.first_name).to.eql("Sebastian")
      pm.expect(item).to.have.property('first_name')
      pm.expect(item).to.have.property('last_name')
      pm.expect(item).to.have.property('email')
      pm.expect(item).to.have.property('id')
    })
})

This will work based on the data set you provided.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80