如何根据空手道条件检查特定值

How to check a particular value on basis of condition in karate

目标:匹配 check 值对于 API

中的 123S123O 响应是正确的

首先检查此位置 x.details[0].user.school.name[0].codeable.text 的值是否为 123S 然后检查 x.details[0].data.check 值是否为 abc

然后检查此位置 x.details[1].user.school.name[0].codeable.text 上的值是否为 123O 然后检查 x.details[1].data.check 是否为 xyz

数组间的响应改变它不是强制性的第一个元素是123S有时APIreturns123O作为第一个数组响应。

示例 JSON.

{
    "type": "1",
    "array": 2,
    "details": [
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "yty",
                                        "condition": "check1"
                                    }
                                ],
                                "text": "123S"
                            }
                        }
                    ]
                },
                "sample": "test1",
                "id": "22222"
            },
            "data": {
                "check": "abc"
            }
        },
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "def",
                                        "condition": "check2"
                                    }
                                ],
                                "text": "123O"
                            }
                        }
                    ]
                },
                "sample": "test",
                "id": "11111"
            },
            "data": {
                "check": "xyz"
            }
        }
    ]
}

我在 Postman 中的表现如何,但如何在空手道中复制相同的内容?

var jsonData = pm.response.json();

pm.test("Body matches string", function () {
    for(var i=0;i<jsonData.details.length;i++){
        if(jsonData.details[i].user.school.name[0].codeable.text == '123S')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('abc');
        }
        if(jsonData.details[i].user.school.name[0].codeable.text == '123O')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('xyz');
        }
    }
});

2 行。这会处理任意数量的查找值组合 :)

* def lookup = { '123S': 'abc', '123O': 'xyz' }
* match each response.details contains { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } }