0

I am trying to validate GET method API response JSON using Postman. I have tried some of online suggestion but nothing worked for me.

In my testcase i am validating that List tag is showing or not.

There is some condition in backend, if that condition is true then only "List" will show in response. so, I just wanted to verify that if its true then its showing in response.

{
        "Documents": [
            {
                "DocID": "0",
                "DocName": "abc",
                "DocDescription": "abdDescription",
                "DocType": "Document",
                "List": [
                    {
                        "Name": "Full Name",
                        "Value": "Tom"
                    },
                    {
                        "Name": "Mo_Number",
                        "Value": "0001112222"
                    },
                    {
                        "Name": "Address",
                        "Value": "000111222"
                    },
                    {
                        "Name": "Country",
                        "Value": "US"
                    },
                    {
                        "Name": "City",
                        "Value": "NY"
                    },
                    {
                        "Name": "Occupation",
                        "Value": "Programming"
                    }
                ]
            }
        ]
    }
4
  • 1
    What does "showing properly" mean? What data do you expect in the response? Or do you expect a different format? How do you know this response is correct?
    – pavelsaman
    Jan 20 at 12:47
  • There is some condition in backend, if that condition is true then only "List" will show in response. so, i just wanted to verify that if its true then its showing in response. Jan 20 at 13:26
  • 1
    What do you want to validate - what are you trying to check? Additionally, what have you tried so far and what were the results? Jan 20 at 13:28
  • I have tried code pm.test('Check nested Id data type', () => { _.each(pm.response.json().values, (topLevelItem) => { _.each(topLevelItem.values, (nestedItem) => { pm.expect(nestedItem.id).to.be.a('string') }) }) }) but its shows pass anyway whether "List" array shows or not Jan 20 at 14:09

2 Answers 2

2

You solution in the comment section:

pm.test('Check nested Id data type', () => {
  _.each(pm.response.json().values, (topLevelItem) => {
    _.each(topLevelItem.values, (nestedItem) => {
      pm.expect(nestedItem.id).to.be.a('string')
    });
  });
});

has a few problems:

  • if there're no values, those loops won't run, so there's nothing to check, therefore it'll pass
  • you mention id property, but I see no id property in your example JSON in your question
  • your check is named Check nested Id data type, but you're asking (from the comment section):

There is some condition in backend, if that condition is true then only "List" will show in response. so, I just wanted to verify that if it's true then it's showing in response.

All in all, people are confused because it seems you're asking more than just one question.


Going back to the problem of checking that List property is in the response. You can check that in JavaScript and Postman like so:

const resBody = pm.response.json();

pm.test("Response has 'List' property", function () {
  pm.expect(resBody.Documents[0]).to.have.ownProperty('List');
});

You also might want to check it's an object:

pm.test("'List' property is object", function () {
  pm.expect(resBody.Documents[0]).to.be.an('object');
});

And you said "only" List:

pm.test("'Documents' has only one property", function () {
  pm.expect(Object.keys(resBody.Documents[0]).length).to.eq(1);
});

All three checks might be problematic because they consider only the first Documents array item. I'll leave the rest to you, you can edit these checks to check all Documents array items if there are more.

I recomment going through Postman documentation, the actually mention all these checks there (and much more) and give you examples. You can check their Writing tests docs and also Chai assertion library.

0

I have a follow up question on this. What if I would like to store "County's" value in a local variable?

I mean just this "US" part from the following object:

                {
                    "Name": "Country",
                    "Value": "US"
                },

This is a real challenge because of all these arrays and the fact that "List" array contains objects which have identical elements. So, "Name" and "Value" are repeating 6 times.

But, I do not want to use this method bellow because the order of those objects may change. In this case "Country" is at third position [3], but it could be on [4] or any other position. So not like this bellow:

const response = pm.response.json();
const country = response.map(data => data.Documents[0].List[3].Value);
pm.variables.set('extractedCounty', country);

I guess that filter function should be used, but I have no idea how.

I am not an expert, so I may made a mistake in this test script I just wrote here, but hopefully you are getting the point.

Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.