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

ε祈祈猫儿з 提交于 2019-12-18 13:19:28

问题


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


回答1:


Did you try

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

?




回答2:


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.




回答3:


This works as of Mar-2019:

pm.test("To Check if Value is Null", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.<yourfield>).not.eq(undefined);
)};



回答4:


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

tests["Item is not null"] = 
    jsonData.item !== undefined;



回答5:


try this one:

pm.test("your-value is not null", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.your_value).not.eql(null);
});



回答6:


You can access response json like :

    var json = JSON.parse(responseBody);
    var yourVAr = json.yourVar
    if(yourVar == null){
        //your var is null here
    }



回答7:


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;

}



回答8:


How about:

var jsonData = JSON.parse(responseBody);

tests["Item is not null"] = 
    jsonData.item !== null && 
    jsonData.item !== ' ' && 
    jsonData.item !== 0;



回答9:


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.




回答10:


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



来源:https://stackoverflow.com/questions/42993222/postman-how-to-check-whether-the-field-is-returning-null-in-the-postman-automat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!