I am writing a postman test in JavaScript to assert the scenario below. I have an id 1111
and the response returns an array of ids. I want to write a test to match 1111 to be in one of the ids in the array.
I have tried to use the include function ie
pm.test("The response contains a valid id in the response", function() {
pm.expect(jsonData.results[0].totalId.children).to.include("1111");
});
{
"totalId": "0000",
"children": [{
"id": "888"
},
{
"id": "3323"
},
{
"id": "1111"
}
]
}
Any suggest.
You're trying to check the value from the array children
, hence, you should not use the .totalId.children
, instead you have to do is jsonData.results[0].children
.
As you trying to check the value from the object of an array, you need add custom JavaScript logic to check the values of id
param.
Here is the function you can use in your test script.
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
The function _isContains
checks the value and key name for the each object from the array.
Now you can pass needed input to the function in your test case.
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
This will check from the array of the object with the key id
and value 1111
, if it's available then it will returns true, otherwise returns false.
Final Test Script
var jsonData = pm.response.json();
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
A even simpler solution could be :
const child = jsonData.results[0].totalId.children.find(c => c.id === "1111");
pm.expect(child).exist;
The advantage of this method is that you can easily extend your tests suite to verify extended keys/values (like stated in the example of @Ajeet Shah) :
pm.expect(child.value).is.not.null;
Note : The assumption is that only one item of the array should have the id "1111". Indeed the find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
As @Justinas mentioned you have to check your id it is "111" and you are checking for "1111".Also you have to changet the structure of JSON you are missing "[" in array.
{"totalId":"0000",
"children": [
{
"id": "888"
},
{
"id": "3323"
},
{
"id": "111"
}
]
}
And the most Importaint thing that your test code should be changed to this:
pm.test("The response contains a valid id in the response", function () { pm.expect(jsonData.results[0].totalId.children[2]).to.eql("111"); });
If the JSON object contains null
values, the _isContains
function needs a small modification: && json[key] !== null
:
{
"totalId":"0000",
"children": [
{
"id": "888",
"value": null
},
{
"id": "3323",
"value": null
},
{
"id": "111",
"value": null
}
]
}
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' && json[key] !== null ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
I have a question: Is it possible to right print statement in the following function? If anyone could suggest?
function _isContains(json, keyname, value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.