Postman: How to Check Whether the Field Is Returning Null in the Postman Automation

Check null value in Postman test cases .not.eql() or .not.equal() are not working

First of all, open the console and run the flowing snippet to check the types:

pm.test("user id is present", function() {
console.log(typeof jsonData.data[0].userId);
console.log(typeof jsonData.data[0]);
console.log(typeof jsonData);
}

If you get undefined for jsonData.data[0].userId, you need change your test to assert null and undefined:

pm.test("user id is present", function() {
pm.expect(jsonData.data[0].userId).to.exist //check if it is not equal to undefined
pm.expect(jsonData.data[0].userId).to.not.be.null; //if it is not equal to null
}

How to check the field data type (only) using postman?

You need to use typeof function which returns the type of unevaluated operand.

var body = JSON.parse(responseBody);
tests["sign_in_count is number"] = typeof(body.data.sign_in_count) === "number";
tests["firstname is string"] = typeof(body.data.firstname) === "string";

How to assert null values in Postman?

you can use

pm.expect(data.Approval).to.be.undefined;

or

pm.expect(data.Approval).to.be.null;

depending on your needs.

You can also take a look at the chai reference for this topic.

How to sanitize a [null] array input for my API program?

Instead of checking, you may simply filter out all NULL values before other works

if (!isset($request->staff_ids) || $request->staff_ids === null) {
$request->staff_ids = array(); // default to empty array if is not set or is null
}

if (!empty($request->staff_ids)) {
$request->staff_ids = array_filter($request->staff_ids, function ($id) {
return ($id !== null);
});

if (!empty($request->staff_ids)) {
// ...
}
}

Postman: How to check the data type of each value in a response

This should do the check for you:

pm.test('Not contain numbers', () => {
var jsonData = pm.response.json()
for (i = 0; i < jsonData.teamPermissions.length; i++) {
pm.expect(jsonData.teamPermissions[i]).to.not.be.a('number')
}
})

Here's what the check will do if a number is part of the array, I've logged out the types so you can see what it's checking against.

Postman

Another alternative is to use Lodash, it's a build-in module for the Postman native app. This code will run the same check as the one above:

pm.test('Not contain numbers', () => {
_.each(pm.response.json().teamPermissions, (arrItem) => {
pm.expect(arrItem).to.not.be.a('number')
})
})


Related Topics



Leave a reply



Submit