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

后端 未结 10 993
借酒劲吻你
借酒劲吻你 2020-12-31 05:57

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

相关标签:
10条回答
  • 2020-12-31 06: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);
    });
    
    0 讨论(0)
  • 2020-12-31 06:42

    You can access response json like :

        var json = JSON.parse(responseBody);
        var yourVAr = json.yourVar
        if(yourVar == null){
            //your var is null here
        }
    
    0 讨论(0)
  • 2020-12-31 06:47

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-31 06:51

    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);
    )};
    
    0 讨论(0)
提交回复
热议问题