如何使用邮递员查找 API 响应中返回的节点数?

How to find the number of nodes returned in an API response using postman?

假设以下是 API 响应:

{
    "content": 
    [
        {
            "id": 90008,
            "capacity": 2,
            "manufacturer": "NISSAN",
            "model": "Sunny",
            "comment": "Nice Compact car.",
            "features": {
                "high_grade": false,
                "normal_grade": true
            },
            "images": [
                {
                    "type": "OUTSIDE",
                    "url": "http://some-image-1.jpg"
                },
                {
                    "type": "INSIDE",
                    "url": "http://some-image-2.jpg"
                }
            ],
            "links": []
        },
        {
            "id": 90009,
            "capacity": 7,
            "manufacturer": "Audi",
            "model": "Q7",
            "comment": "Very good leg space!",
            "features": {
                "high_grade": true,
                "normal_grade": false
            },
            "images": [
                {
                    "type": "OUTSIDE",
                    "url": "http://some-image-1.jpg"
                },
                {
                    "type": "INSIDE",
                    "url": "http://some-image-2.jpg"
                }
            ],
            "links": []
        }
    ],
    "page": {
        "size": 20,
        "total_elements": 2,
        "total_pages": 1,
        "number": 0
    }
}

现在在postman中,如何查到content返回的parent/main节点总数为8个,如下:

  1. id
  2. 容量
  3. 制造商
  4. 型号
  5. 评论
  6. 特征
  7. 图片
  8. 链接

我尝试了以下但失败了:

pm.test("Check total no. of nodes in all content is 8", () => {
    for (i = 0; i < jsonData.content.length; i++) { 
        pm.expect(Object.keys(jsonData.content[i]).length).to.equal(8);
    }
});

只是缺少您的代码:

var jsonData = pm.response.json()

测试会在 运行 时告诉您,并给您 Check total no. of nodes in all content is 8 | ReferenceError: jsonData is not defined 消息。

所以它应该看起来像这样:

pm.test("Check total no. of nodes in all content is 8", () => {
    var jsonData = pm.response.json()
    for (i = 0; i < jsonData.content.length; i++) { 
        console.log(Object.keys(jsonData.content[i]))
        pm.expect(Object.keys(jsonData.content[i]).length).to.equal(8);
    }
});

您提到 The 0 indexed nodes, such as links, can be ignored 但是您进行测试的方式和您正在检查的内容无论如何都会包括它们。它们也是您附加到问题的列表的一部分。

我添加了行 console.log(Object.keys(jsonData.content[i])) 以在 Postman 控制台中向您展示。