从Postman API中的嵌套响应中检索值

时间:2018-10-10 03:15:10

标签: javascript testing automated-tests postman postman-collection-runner

我一般都不熟悉Postman和编程/脚本,需要一些帮助来从嵌套数组中检索值,并且还需要验证响应中返回的属性数量。我有以下JSON:

{
    "studentsData":[
        {
            "id": 142,
            "firstName": "Kai",
            "lastName": "Carvalho Rodrigues",
            "addressLine1": "4188 Viking Drive",
            "addressLine2": "",
            "state": "VA",
            "city": "Fairview",
            "postalCode": "43736S",
            "country": "US",
            "emergencyContacts":[
                {
                    "firstName": "Ruby",
                    "lastName": "Wilfred",
                    "phoneNumber": "5541-766-XXXX"
                },
                {
                    "firstName": "Isaac",
                    "lastName": "Jeanneret",
                    "phoneNumber": "5541-761-XXXX"                   
                }         
            ]
        },
        {
            "id": 143,
            "firstName": "Kai",
            "lastName": "Yewen",
            "addressLine1": "4956 Alexander Avenue",
            "addressLine2": "",
            "state": "CO",
            "city": "Denver",
            "postalCode": "94607",
            "country": "US",
            "emergencyContacts":[
                {
                    "firstName": "Matilda",
                    "lastName": "McAdam",
                    "phoneNumber": "988-445-XXXX"
                }
            ]
        }            
    ]
}

我在这里尝试: 在studentData下打印名字 验证响应中的名字计数 打印紧急联系人下的电话号码列表 验证响应中的电话号码计数 在Postman Console的紧急联系人下打印名字和电话号码。

//打印响应中的所有名字

var jsonData = JSON.parse(responseBody);
var name = jsonData.studentsData;
for (var i = 0; i <= name.length; i++ ) {
console.log(" The list of students' first names are: "+name);

//验证响应中的名字计数

var jsonData = JSON.parse(responseBody);
tests["Total number of students = 2"] = jsonData.studentsData.length === 2;

//打印紧急联系人下的电话号码列表

var responseData = JSON.parse(responseBody);
var i;
var j;
var contactNumbers = jsonData.studentsData[i].emergencyContacts[j];

for (var i = 0; i <= contactNumbers.length; i++ )
    for (var j = 0; j <= contactNumbers.length; j++) {
        console.log(" The list of emergency contacts are: "+contactNumbers);
    }
}

//验证响应中的紧急联系人数

    var jsonData = JSON.parse(responseBody);
    var i;
    var contactNumbers = jsonData.studentsData[i].emergencyContacts[j];
    for (i=0; i <= contactNumbers.length)
       for (var j = 0; j <= contactNumbers.length; j++){
    tests["Total number of emergency contacts = 4"] = jsonData.studentsData[I].emergencyContacts[j].length === 4;
    };

在邮递员控制台中的紧急联系人下打印名字和电话号码-无法解决这种情况

0 个答案:

没有答案