Postman: How to check whether the field is returning null in the Postman automation
Asked Answered
B

12

26

I have tried with !== null, but it is returning PASS even when the field is returning 0 or empty string.

Beichner answered 24/3, 2017 at 6:55 Comment(0)
B
36

This works as of Mar-2019:

pm.test("Check if value is null", function() {
  var jsonData = pm.response.json();
  pm.expect(jsonData.<yourField>).not.eq(undefined);
});
Bradski answered 25/3, 2019 at 21:49 Comment(2)
.is.not.undefined also works. If you really care about null as well, use is.not.oneOf([null, undefined]).Vilayet
This is the 5th version of this that I've had to look for (no exaggeration). Works for a couple of weeks, then stops. These solutions don't work for me. Neither does .to.eql(null) which is what I had before googling it, which is no longer working for me as of today.Kuehnel
E
17

Did you try

pm.expect(response.your_field).to.eql(null);

?

Expensive answered 28/3, 2018 at 18:32 Comment(1)
OP asked for Not equalInterfere
A
12

If you are checking the Id of the first item returned in a list, you could use not.equal(null):

pm.expect(pm.response.json().value[0].Id).not.equal(null);

Note that the word "equal" is fully spelled out, although the shortened "eql" does work.

Astronaut answered 31/8, 2018 at 18:42 Comment(2)
Not sure if .to is redundant, but that is whate I use.Kussell
Good answer, easily checks non null for json element.Estrade
D
3

try this one:

pm.test("your-value is not null", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.your_value).not.eql(null);
});
Dispel answered 24/5, 2018 at 4:43 Comment(0)
P
3

Postman doesn't reference non-existent paths as null, but as undefined.

pm.expect(JsonResponse.FAKE.PATH).not.eql(undefined);

This test should fail, as this fake json path is actually undefined.

Phillida answered 18/2, 2019 at 22:10 Comment(0)
D
1

I faced similar issue. But checking it in following way worked for me

tests["Item is not null"] = 
    jsonData.item !== undefined;
Dialogize answered 9/11, 2017 at 3:20 Comment(0)
B
0

You can access response json like :

    var json = JSON.parse(responseBody);
    var yourVAr = json.yourVar
    if(yourVar == null){
        //your var is null here
    }
Bugle answered 24/3, 2017 at 7:0 Comment(0)
E
0

I have done similar, this is one part of the code and its works well Check this code

var jsonData = JSON.parse(responseBody);

for(i=0; i<jsonData.data.length; i++){
tests["due date is between given date range"] = jsonData.data[i].duedate < environment.Cenddate && jsonData.data[i].duedate > environment.Cstartdate;

tests["response body has department name"] = jsonData.data[i].department.name !== null;

}
Eckmann answered 31/3, 2017 at 4:54 Comment(0)
T
0

How about:

var jsonData = JSON.parse(responseBody);

tests["Item is not null"] = 
    jsonData.item !== null && 
    jsonData.item !== ' ' && 
    jsonData.item !== 0;
Trainload answered 16/10, 2017 at 22:5 Comment(0)
I
0

You gotta place the + [i] after the function you gonna validate on the tests[] so only it will return valid statements on each array. For example,

function checkIsNull() {
    var items = json.data.pois.items
    var PlaceIdIsNull = true;
    var subTypeExtraIsNull = true;
    var websiteIsNull = true;

    for (var i = 0; i < items.length; i++) {
    if (items[i].placeId !== null) {
        PlaceIdIsNull = false;
    }
    if (items[i].subTypeExtra !== null) {
        subTypeExtraIsNull = false;
    }
    if (items[i].website === null) {
        websiteIsNull = false;
        tests['website is null only happened on these arrays' + [i]] = true;
        }
        tests['Place ID is null '] = PlaceIdIsNull
        tests['subTypeExtra is null '] = subTypeExtraIsNull
}
}
       checkIsNull();

Result:


PASS Place ID is null

PASS subTypeExtra is null

Icelander answered 1/7, 2019 at 4:5 Comment(0)
S
0

Try this:

let jsonData = pm.response.json();

pm.expect(jsonData["yourfield"]).to.eq(null);
Spermatozoon answered 5/7, 2022 at 5:47 Comment(0)
H
0

This works as at Sept.19, 2022

     pm.test("Check that response body is not null", ()=>{
   pm.response.to.be.have.body
    })
Histogen answered 19/9, 2022 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.