【发布时间】:2021-11-21 12:43:45
【问题描述】:
目标:匹配 check 值对于 API 中的 123S 和 123O 响应是正确的
首先检查此位置上的值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有时API返回123O作为第一个数组响应。
示例 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');
}
}
});
【问题讨论】:
标签: karate