With my api I get an array in json form as
{
“suggestions”: [
{
“value”: “jacket dress”
},
{
“value”: “jacket”
},
{
“value”: “jacquard towel”
},
{
“value”: “jackets”
},
{
“value”: “jacquard”
}
]
}
I need to validate each value of “value:” with some expcted array of data. Being Postman newborn , how can this be achieved. Where can i define my actual data? and how? .
can i have this in pre request script, let jac=[{value:“jacket dress”},{value:“jacket”},{value:“jacquard towel”},{value:“jacquard”},{value:“jackets”}];. Im lost pls help.
So here we need two arrays. One is user defined, where you are planning to define the expected. And another array where the results from the response is stored in it. Finally we need to verify each other.
Please find below the snippets.
Under pre-request script you can define the array with expected values:
var result = ["jacket dress","jacket","jacquard towel","jackets","jacquard"];
pm.environment.set("result", result);
Under tests section,
var resp = pm.response.json();
console.log(resp);
respArray = [];
for (var i=0; i<resp.suggestions.length; i++)
{
respArray.push(`${resp.suggestions[i].value}`);
//console.log(respArray);
pm.environment.set('respArray', (respArray));
}
console.log(respArray);
console.log(pm.environment.get("result"));
pm.test("Validate the array", function () {
var _ = require('lodash');
_.isEqual(respArray, pm.environment.get("result"));
});
Sorry but I was too fast in replying now I changed my input param and still see that test has passed.
The response body {
“suggestions”: [{
“value”: “jean’s”
},
{
“value”: “jean’s ladies”
},
{
“value”: “jeans men”
},
{
“value”: “jeans kids”
},
{
“value”: “jeans high waist”
}
]
}
Comparing with result variable, It should fail
Oh okay, even I didn’t check the response subset properly. Well in that case, I believe you need to iterate through the expected result and store the values in an array and then you need to validate against the actual result. So I believe we can follow the first approach to make it simple.
Oh I get that, can you try the below snippet please
pm.test("Validate the array", function () {
if (JSON.stringify(respArray) === JSON.stringify(pm.environment.get("result"))){
console.log('Both arrays are equal!!');
} else {
pm.expect.fail('This failed because Arrays are not equal');
console.log('Arrays are not equal');
}
});
I want to check the whole response body has a parameter with datatype.
Ya could you tell me what will be the best practice for this.
currently, I m converting JSON response from the swagger doc and in postman that response body i m storing in a variable like this…
var schema=
{
“type”: “array”,
“items”: [
{
“type”: “object”,
“properties”: {
“modifiedOn”: {
“type”: “string”
},
“id”: {
“type”: “string”
},
“header”: {
“type”: “string”
},
“content”: {
“type”: “string”
},
“wordsCount”: {
“type”: “integer”
},