3

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

Muzna Zafar
  • 45
  • 1
  • 6

1 Answers1

1

You can grab the JSON data from the request like this:

const requestJson = JSON.parse(pm.request.body.raw);

(This assumes you're using a RAW body in Postman.)

Then, you can compare the response's data fields with the original request's data fields:

const requestJson = JSON.parse(pm.request.body.raw);
const responseJson = pm.response.json();

for (const [reqKey, reqValue] of Object.entries(requestJson.data)) {
    pm.expect(responseJson.data[reqKey]).to.eql(requestJson.data[reqKey]);
}

From there, you can add whatever checks you want to do on the rest of the response.

jknotek
  • 1,778
  • 2
  • 15
  • 23
  • I don't quite understand what responseJson.data[reqKey] is?..i tried to log its value but seem to get nothing. Also i feel it's not quite getting all the key value pairs from request body and check them against response keys. Can you please elaborate your answer? – Muzna Zafar Nov 26 '19 at 22:39
  • @MuznaZafar Please try `console.log(pm.request.body.raw)`, and let me know what it shows. – jknotek Nov 26 '19 at 22:55
  • This is what is shows: `code` {↵ "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"↵ ]↵ }↵ } `code` – Muzna Zafar Nov 26 '19 at 23:10
  • @MuznaZafar Oops, I had a typo (`resposeJson` should have been `responseJson`). I updated my answer with the fix. Can you try again? – jknotek Nov 26 '19 at 23:15
  • I figured out the typo already and corrected it, it still doesn't work for me :( – Muzna Zafar Nov 26 '19 at 23:36
  • @MuznaZafar There was another typo I just fixed. Please try again. If you're still having trouble, it would be helpful to `console.log` more of the variables to see what you're working with. It's harder to do that on my end because I don't have access to your API. – jknotek Nov 27 '19 at 02:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203141/discussion-between-muzna-zafar-and-jknotek). – Muzna Zafar Nov 27 '19 at 03:04