简体   繁体   English

如何使用邮递员测试检查API响应中的一个节点在所有对象下是否具有相同的值?

[英]How to check whether one node in API response has same value under all object using postman tests?

Suppose a API request fetches a users id, email address and their designated role. 假设API请求提取用户ID,电子邮件地址及其指定的角色。 Sample API Request below: 以下示例API请求:

GET: /v1/users HTTP/1.1
Content-Type: application/json
Authorization: bearer {access_token}

For the above request, the following is the response: 对于上述请求,以下是响应:

{
    "content": [
        {
            "id": 1,
            "email": "random@random.com",
            "full_name": "AlbusDumbledore",
            "role": "OWNER"
        },
        {
            "id": 40,
            "email": "random1@random1.com",
            "role": "OWNER"
        }
],
    "last": false,
    "total_elements": 2,
    "total_pages": 1,
    "sort": null,
    "first": true,
    "number_of_elements": 2,
    "size": 20,
    "number": 0
}

Now, what will be the test in postman to make sure that all the returned values under role node is equal to OWNER? 现在,postman中的测试将确保角色节点下的所有返回值等于OWNER?

You could add something like a loop to check for it? 你可以添加类似循环的东西来检查它吗?

pm.test("Check role equals OWNER", () => {
    var jsonData = pm.response.json()
    for (i = 0; i < jsonData.content.length; i++) { 
        pm.expect(jsonData.content[i].role).to.equal('OWNER')
    }
})

This should work to check the value of each role property, if the schema response you posted is correct. 如果您发布的架构响应正确,这应该可以检查每个role属性的值。

I changed one of the values and ran the test again to show you it working - This will show the failure in Postman if the property is not equal to 'OWNER'. 我更改了其中一个值并再次运行测试以显示它正常工作 - 如果属性不等于'OWNER',这将显示Postman中的失败。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用邮递员验证响应正文中的节点是否具有唯一值? - How to verify if a node in response body has unique value with postman? 如何检查所有特定对象在数组中是否具有相同的值? - How to check if all specific object has a same value in array? 如何检查输入框是否具有相同的值 - How to check whether input box has same value 如何在 postman 测试中检查请求正文是否有密钥 - How can I check request body has a key or not in postman tests 如何检查 object 是否有钥匙? - How to check in an object whether its has the key or not? 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript? 当属性名称超过一个单词时如何检查邮递员响应数组值 - How to check postman response array value when attribute name more than one word 如何检查脚本是否在 Node.js 下运行? - How to check whether a script is running under Node.js? 如何检查数组是否具有key:value? - how to check in an array whether it has a key:value or not? JavaScript 如何检查一个 JS 对象的所有键和值是否存在于另一个 JS 对象中? - How to check whether all the key & value of a JS Object exist in the another JS object in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM