Having JSON response with nested elements. I would like to validate it against JSON test data using Postman and test functions.
For the moment I can easily reach it by hardcoding:
const jsonData = pm.response.json();
pm.expect(jsonData.items[0].title).to.eql(pm.iterationData.get("title"));
pm.expect(jsonData.items[1].title).to.eql(pm.iterationData.get("title"));
here is my JSON test data file:
[
{
"title": "Title1"
},
{
"title": "Title2"
}
]
Question is how to validate it in different way, but still using JSON test data structure? e.g. if I will have like 100 titles to verify in JSON test data, no need to of course put 100 same lines…
Thanks in advance
2
Answers
Iterate over the JSON test data
Use a loop to iterate through the test data and validate the corresponding elements in the response.
You can add conditions accordingly
For Postman scripts:
Hope this helps