POSTMAN:如果密钥:值存在,如何检入Json响应数组

时间:2018-04-06 05:56:04

标签: postman

如果key:value存在,我想检查我的JSON响应数组。 和我想如果找到键:值从循环中退出并获得测试结果。 你能帮我支持怎么做吗?

e.g。我有以下回应:

[
    {
        "persID": "personID_1",
        "vip": false,
        "account": {
            "contactName": "value1",
        },
        "premium": {
            "Name": "value2",
            "Director": "value3",
            "company": "value7",
            "homePage": "value6",
            "address": {
                "country": "value8",
                "city": "value9"
            },
            "photo": value10
        }
    },
    {
        "persID": "personID_2",
        "vip": false,
        "account": {
            "contactName": "value11",
        },
        "premium": {
            "Name": "value12",
            "Director": "value13",
            "company": "value17",
            "homePage": "value16",
            "address": {
                "country": "value18",
                "city": "value19"
            },
            "photo": value110
        }
    },
    .....
    .....// dynamic response can be "n" elements!!!!
    ]

我想检查此数组中是否存在带有(“persID”:“personID_3”)的键值。 (例如,对于persID_3,如果结果失败,并且persID_2 已通过

我尝试过以下但没有结果:

var jsonArray = pm.response.json();
var persID = "persID_3";
pm.test("2. tets check if persdID exist in array", function () {
 var i=0;
 for(i; i<jsonArray.length;i++){

                pm.expect(jsonArray).to.have.property(jsonArray[i].persID, persID);
                // pm.expect(jsonArray[i]).to.have.property(jsonArray[i].persID, persID);

 }
});

也尝试过 var jsonArray = JSON.parse(responseBody); 和 pm.expect(jsonArray [i])。to.have.property(jsonArray [i] .persID,persID);

但没有结果

感谢您的支持。

3 个答案:

答案 0 :(得分:0)

您可以使用Lodash遍历数组并检查响应中的值:

pm.test('Matches personID property value', () => {
    _.each(pm.response.json(), (arrItem) => {
        if (arrItem.persID === 'personID_2') {
            throw new Error(`Array contains ${arrItem.persID}`)
        }
    })
});

如果personID_2属性存在但是这个值可能是您喜欢的任何值,那么测试将失败。我使用了您的响应数据并创建了一个快速的API路径来向您展示这一点。

Postman

我将值添加到错误消息中,以便它在测试输出上更具描述性。

答案 1 :(得分:0)

Postman使用chai 库的扩展实现。 https://github.com/postmanlabs/chai-postman

你可以使用这样的东西来检查

我正在硬编码响应对象。您可以使用pm.response ...

从API响应中解析它
let resp = [{
    "persID": "personID_1",
    "vip": false,
    "account": {
        "contactName": "value1",
    },
    "premium": {
        "Name": "value2",
        "Director": "value3",
        "company": "value7",
        "homePage": "value6",
        "address": {
            "country": "value8",
            "city": "value9"
        },
        "photo": "value10"
    }
}, {
    "persID": "personID_2",
    "vip": false,
    "account": {
        "contactName": "value11",
    },
    "premium": {
        "Name": "value12",
        "Director": "value13",
        "company": "value17",
        "homePage": "value16",
        "address": {
            "country": "value18",
            "city": "value19"
        },
        "photo": "value110"
    }
}];

pm.test('Finding person ID', function() {
    let personId = "personID_1";

    _.forEach(resp, (respObj, index) => {
        if (respObj.persID === personId) { // or do the vice-versa
            console.log('Found', respObj);
            throw new Error('Found it!'); // This will make the test fail
        }
    });
    // If not found then the test will pass... 
});

答案 2 :(得分:0)

谢谢,Danny&amp; Sivcan, 在你的支持下,我可以找到我需要的东西,也许不是很好的编码,但它的工作原理。我无法使用你的功能(来自Danny)如何离开循环。所以这个解决方案对我有用:

// Check if the netry with Key:value persID:regDB_persID exist in Array
var jsonArray = pm.response.json();
var var_regDB_persID = pm.environment.get("regDB_persID");  // environment var from registration

var flag = false;
 // var i=0;
    for(var i=0; i<jsonArray.length;i++){
        _.each(pm.response.json(), (arrItem) => {
            if (arrItem.persID === (var_regDB_persID)){
                // throw new Error(`Array contains ${arrItem.persID}`)
            console.log(flag = true);
            }
        });
    }

    pm.test('2. Get Info for all accepted User from Admin - Matches personID property value', function(){
        if (flag === true) {                        
            return;}  // persID was in response with value   
            else {
             throw new Error("Array Does NOT contains" + var_regDB_persID)}
    });

在这种情况下,如果存在具有值的属性,它将通过案例。