0

How to know if the objects exist in postman like below, i want to check all the parameters. if all of them are returned properly

[
    {
        "id": "MnnRVEifcngi2",
        "givenName": "Witting and Sons",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Fri Jan 05 2018 07:54:09 GMT+0000 (UTC)",
        "updatedAt": "Tue Oct 23 2018 22:25:54 GMT+0000 (UTC)",
        "tags": [
            "Web",
            "Paradigm"
        ]
    },
    {
        "id": "E7z9UujhROF2L",
        "givenName": "Block Group",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Mon Feb 05 2018 11:50:00 GMT+0000 (UTC)",
        "updatedAt": "Wed Oct 24 2018 13:29:35 GMT+0000 (UTC)",
        "tags": [
            "Brand",
            "Web"
        ]
    },
    {
        "id": "MzbqnzFImpbkf",
        "givenName": "Dickinson - Ziemann",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Fri Feb 02 2018 07:00:32 GMT+0000 (UTC)",
        "updatedAt": "Tue Oct 23 2018 18:11:30 GMT+0000 (UTC)",
        "tags": [
            "Applications"
        ]
    },
    {
        "id": "3-vqC_QG5Up8r",
        "givenName": "Lindgren - Mitchell",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Sun May 06 2018 07:24:02 GMT+0000 (UTC)",
        "updatedAt": "Wed Oct 24 2018 10:10:23 GMT+0000 (UTC)",
        "tags": [
            "Branding",
            "Mobility",
            "Functionality"
        ]
    },
    {
        "id": "8dUhM_0j5vloD",
        "givenName": "Schmitt LLC",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Wed Sep 12 2018 01:16:49 GMT+0000 (UTC)",
        "updatedAt": "Wed Oct 24 2018 03:40:15 GMT+0000 (UTC)",
        "tags": [
            "Accounts",
            "Data"
        ]
    },
    {
        "id": "zl_43QRBDWBnW",
        "givenName": "Barton - Bauch",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Tue Dec 05 2017 14:12:00 GMT+0000 (UTC)",
        "updatedAt": "Tue Oct 23 2018 19:36:52 GMT+0000 (UTC)",
        "tags": [
            "Response",
            "Accountability",
            "Identity"
        ]
    },
    {
        "id": "kxgqxbXBS53_2",
        "givenName": "Lind Inc",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Tue May 22 2018 16:37:33 GMT+0000 (UTC)",
        "updatedAt": "Wed Oct 24 2018 06:35:53 GMT+0000 (UTC)",
        "tags": [
            "Communications",
            "Brand"
        ]
    },
    {
        "id": "xWAIuoDY5icIl",
        "givenName": "Gutkowski - Hickle",
        "logo": "http://lorempixel.com/640/480/business",
        "createdAt": "Tue Jun 19 2018 21:29:59 GMT+0000 (UTC)",
        "updatedAt": "Wed Oct 24 2018 14:02:39 GMT+0000 (UTC)",
        "tags": [
            "Metrics",
            "Infrastructure",
            "Accounts"
        ]
    }
]

This is what i am trying but getting an error:

const jsonData = pm.response.json();

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

I want to verify - id, givenName, logo, createdAt, UpdatedAt, tags and all are present and want to make a global function so in other tests I can just call it once

Eins
  • 37
  • 7

2 Answers2

2

To fix your error, try to loop through your array of object i.e jsonData like below. By the way I didn't play with postman global function creation. Hope this link will help you on how to make/use it as global How to Write Global Functions in Postman

pm.test('Has data', () => {
  jsonData.forEach(row => {
    pm.expect(row).to.have.property('id');
    pm.expect(row).to.have.property('givenName');
    pm.expect(row).to.have.property('logo');
    pm.expect(row).to.have.property('createdAt');
    pm.expect(row).to.have.property('updatedAt');
    pm.expect(row).to.have.property('tags');
  })    
});
n-verbitsky
  • 552
  • 2
  • 9
  • 20
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • thanks that works. How can i set it globally in prescript and just call it in every request? – Eins Oct 25 '18 at 01:12
  • thanks that works. How can i set it globally in prescript and just call it in every request? – Eins Oct 25 '18 at 01:40
0

For checking all parameters in response you should consider using built-in tv4.validate, which stands for Tiny Validator (for v4 JSON Schema) - it validates simple values and complex objects against provided schema.

In your particular case you would do something like this:

const jsonData = pm.response.json(); 

const schema = {
  items: {
    type: "object",
    properties: {
        id: {type: "string"},
        givenName: {type: "string"},
        logo: {type: "string"},    
        createdAt: {type: "string"},
        updatedAt: {type: "string"},
        tags: {type: "array"}
    },
    required:["id","givenName","logo", "createdAt", "updatedAt", "tags"]
  }
};

pm.test('Schema is valid', () => pm.expect(tv4.validate(jsonData, schema)).to.be.true);

More examples of how to use tv4 can be found in its repo.

n-verbitsky
  • 552
  • 2
  • 9
  • 20