1

In given response snippet "parentName" type is sometimes null or sometimes string. How to check/write testcase to check the typeof string as well as null at a time.

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || null;

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || "null";
"demo": [
            {
                "id": 68214,
                "specializationId": 286,
                "name": "Radiology",
                "parentName": null,
                "primary": true
            }
        ],

How to handle this kind of condition in postman (null & string).

Francois Borgies
  • 2,378
  • 31
  • 38
scorpio7
  • 13
  • 1
  • 6

1 Answers1

0

I would not recommend if elseif in Postman test case. Postman has inbuilt feature for schema checking, you can use that and can achieve the same result without if else.

First, I'm considering following as a response:

{
    "demo": [{
        "id": 68214,
        "specializationId": 286,
        "name": "Radiology",
        "parentName": null,
        "primary": true
    }]
}

Postman test should be:

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "parentName": {
            "type":["string", "null"]
        }
    }
};

pm.test('Verify parentName is string', function() {    
    var resParentName =  pm.response.json().demo[0].parentName;
    pm.expect(ajv.validate(schema, {parentName: resParentName})).to.be.true;
});

Edit: Validate full response, not just first item. Also check if parentName is present in the response or not.

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "demo":{
            "type": "array",
            "items": {
                 "properties": {
                     "id":{ "type": "integer" },
                     "specializationId":{ "type": "integer" },
                     "name":{"type": "string"},
                     "parentName":{
                         "type":["string", "null"]
                     },
                     "primary":{"type": "boolean"}
                 },
                 "required": [ "parentName"]
            }
        }
    }
};

pm.test('Validate response', function() {
    pm.expect(ajv.validate(schema, pm.response.json())).to.be.true;
});
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Hey need small help i am getting error `Error: Cannot find module 'ajv'` then I tried `npm install ajv` but still getting same error. please tell what should i do. – scorpio7 Nov 08 '19 at 09:24
  • @Akshay77: What is your Postman version. There is no need to install npm package – Divyang Desai Nov 08 '19 at 09:45
  • Wouldn't that only check the first object, what if there were more? If parentName was removed from the response, I'm sure that would still pass. – Danny Dainton Nov 08 '19 at 10:21
  • Thanks @DannyDainton for pointing out the edge cases I had missed. Answer updated! Let me know if anything still missing :) – Divyang Desai Nov 08 '19 at 11:03