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.
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
Wait for Http Request in Angular
How to Manipulate a Select Options Based on Another Select Option in HTML
Regex Pattern to Match At Least 1 Number and 1 Character in a String
Detect If a Field Is Updated With JavaScript or Jquery
Pass Input Tag Value to as Href Parameter
How to Trigger Change of Option in Dropdown When Options Can Have Same Value
How to Change Data Due to Selected Value in Select Vue
Sequelize How to Check If Entry Exists in Database
Re-Render Same Component on Url Change in React
Call Js Function After Div Is Loaded
Regex to Remove Certain Characters At the Beginning and End of a String
Click on Pseudo Element Using Selenium
How to Cancel Ongoing Http Requests When There's a New Requests in Angular 6 With Rxjs 6
How to Add Class on Specific Div When Scroll
Stylesheet Not Loading Because Mime Type Is Text/Html
How to Pass Array Variable from View to Controller in Spring MVC