Postman: How to check whether the field is returni

2020-02-26 08:07发布

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

10条回答
甜甜的少女心
2楼-- · 2020-02-26 08:36

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

查看更多
Root(大扎)
3楼-- · 2020-02-26 08:40

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);
)};
查看更多
SAY GOODBYE
4楼-- · 2020-02-26 08:40

How about:

var jsonData = JSON.parse(responseBody);

tests["Item is not null"] = 
    jsonData.item !== null && 
    jsonData.item !== ' ' && 
    jsonData.item !== 0;
查看更多
Ridiculous、
5楼-- · 2020-02-26 08:41

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楼-- · 2020-02-26 08:43

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.

查看更多
一纸荒年 Trace。
7楼-- · 2020-02-26 08:45

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.

查看更多
登录 后发表回答