Javascript Postman:如何在 Postman 自动化中检查该字段是否返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42993222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:35:28  来源:igfitidea点击:

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

javascriptapitestingautomated-testspostman

提问by Tester77

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

我曾尝试使用“!== null”,但即使该字段返回 0 或“”,它也会返回 PASS。

回答by Old Panda

Did you try

你试过了吗

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

?

?

回答by Shiraz

This works as of Mar-2019:

这适用于 2019 年 3 月:

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

回答by myrtle303

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

如果您正在检查列表中返回的第一项的 Id,您可以使用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.

请注意,尽管缩写的“eql”确实有效,但“equal”一词已完整拼写。

回答by cleo

try this one:

试试这个:

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

回答by Mario

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

Postman 不会将不存在的路径引用为 null,而是将其引用为未定义。

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

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

这个测试应该会失败,因为这个假的 json 路径实际上是未定义的。

回答by Ashish Bhatt

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

我遇到了类似的问题。但是按照以下方式检查它对我有用

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

回答by Wayne Yeow

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,

你必须将 + [i] 放在你要在测试 [] 上验证的函数之后,这样它才会在每个数组上返回有效的语句。例如,

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 地点 ID 为空

PASS subTypeExtra is null

PASS subTypeExtra 为空

回答by Nitin Vavdiya

You can access response json like :

您可以访问响应 json,如:

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

回答by Acid

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;

}

回答by John Meyer

How about:

怎么样:

var jsonData = JSON.parse(responseBody);

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