I'm writing tests in POSTMAN against a POST API request by sending JSON body data of the following format:
"data": {
"name": "Amber Joseph",
"dob": "1988-10-13",
"addressLine1": "Ap #770-9459 Quis Av.",
"state": "WA",
"suburb": "Beverley",
"yesNo": false,
"balance": 423.00,
"club": [
"Dance",
"Sports"
],
"activities" : null
"libraryCard": {
"uid": "2d07d77c-8756-43d4-912f-238a2ff567fe"
}
}
I get Response for the request in similar format with some added details:
{
"status": "Success",
"message": "Created new 'Student' record",
"correlationCode": "Z848640-261354",
"type": {
"id": 51247,
"name": "Student",
"slug": "student",
"application": {
"name": "Willow University"
}
},
"data": {
"name": "Amber Joseph",
"dob": "1988-10-13",
"addressLine1": "Ap #770-9459 Quis Av.",
"state": "WA",
"suburb": "Beverley",
"yesNo": false,
"balance": 423.00,
"club": [
"Dance",
"Sports"
],
"libraryCard": {
"uid": "2d07d77c-8756-43d4-912f-238a2ff567fe",
"name": "11206"
}
}
Now i want to achieve two things here: 1. Verify each key in response body does not have null value. Please note i'm sending one key with value as null and it is not returned in response. 2. The value sent in request body for each key is value returned by same key in response body. For instance if "name" key has value "Amber Joseph" then response key "name" also returns "Amber Joseph". But i want to do it for each key. Also Keys can defer everytime for instance i might or might not send it with "name" key hence i need a generic solution that applies to whatever key value pairs i send.
I'm able to loop through by using:
let jsonData = pm.response.json();
let dKey = Object.keys(jsonData);
let dValue = Object.values(jsonData);
for(var i = 0; i < dV.length; i++ ){
pm.expect(dV[i]).to.not.eql(null);
}
But this does not check nested key value pair individually.I specially want to check each key value pair inside the "data" object. Any help would be appreciated.
Thanks