空手道 API 框架有什么方法可以遍历子值
Karate API framework is there any way to iterate through the child values
我的方法取得了 90 分的成功,但是当响应在其中一个子键中有多个条目时,逻辑就会失败,我无法将一个通用逻辑放在适当的位置 运行 适用于所有情况。
响应样本是
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
}
]
}
]
}
现在我的用例说您搜索 John text,然后 items 数组将包含许多对象,其 items.name 或 items.sections.description 应包含 "John" 关键字
我输入的匹配逻辑工作正常,因为我正在遍历项目[].name 和items.sections[].description
当部分[*].description 包含如下多个部分时,主要挑战就来了
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
},
{
"id":1,
"description": "remain smith of the first object"
}
]
}
]
}
逻辑现在应该可以正常工作了
items[].name 或 items.sections[].description(部分 [*].description 的多个条目)
我面临的问题是在迭代项目[].name & items[].sections[*].description
它在单独的数组中为我提供了所有名称和所有 sections.description 我想要的是它应该一一给我。
例如第一个结果集应该在下面给我
[
"John"
]
and
[
"John smith"
"remain smith of the first object"
]
这样我就可以 运行 我现有的逻辑来检查 John 是否有空。目前我的逻辑 运行s 在描述的第一个条目上并且它不检查下一个条目或 section.description 这是下面匹配对象失败的原因,因为 "john" 出现在第二个条目中描述
{
"items": [
{
"id":11,
"name": "SMITH",
"sections": [
{
"id":11,
"description": "SMITH"
},
{
"id":11,
"description": "JOHN Carter"
}
]
}
]
}
我目前使用的匹配逻辑是-
* def matchText =
"""
function (nameArr, sectionArr, matchingWord)
{
for(var i = 0; i < nameArr.length; i++)
var regEx = new RegExp(matchingWord, 'gi')
var nameMatch = nameArr[i].match(regEx)
var secMatch = sectionArr[i].match(regEx)
if (nameMatch ==null && secMatch == null) {
return false;
}
}
return true;
}
"""
* def getName = get response.items[*].name
* def getDescription = get response.items[*].sections[*].description
* assert matchText(getName,getDescription,'john')
因此,当您的名称长度相同时,此逻辑有效 & sections.description 但是当 sections.description 有多个数组时,它不会正确迭代。这是我想将名称视为一个对象而将 sections.description 视为另一个对象的唯一原因,即使其中会有多个子 ID。
示例代码:
Feature: Validation
Scenario:
* def resp =
"""
{
"items": [
{
"id": 11,
"name": "JOHN",
"sections": [
{
"id": 11,
"description": "SMITH"
},
{
"id": 11,
"description": "JOHN Carter"
}
]
}
]
}
"""
* def names = []
* def fun =
"""
function(x){
var json = x.sections;
var temp = x.name
for(var i = 0; i < json.length; i++) {
var obj = json[i];
temp = temp + "," + obj.description
}
karate.appendTo(names, temp);
}
"""
* def items = get resp.items[*]
* karate.forEach(items, fun)
* match each names == '#regex .*JOHN.*'
我的方法取得了 90 分的成功,但是当响应在其中一个子键中有多个条目时,逻辑就会失败,我无法将一个通用逻辑放在适当的位置 运行 适用于所有情况。
响应样本是
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
}
]
}
]
}
现在我的用例说您搜索 John text,然后 items 数组将包含许多对象,其 items.name 或 items.sections.description 应包含 "John" 关键字
我输入的匹配逻辑工作正常,因为我正在遍历项目[].name 和items.sections[].description
当部分[*].description 包含如下多个部分时,主要挑战就来了
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
},
{
"id":1,
"description": "remain smith of the first object"
}
]
}
]
}
逻辑现在应该可以正常工作了 items[].name 或 items.sections[].description(部分 [*].description 的多个条目)
我面临的问题是在迭代项目[].name & items[].sections[*].description
它在单独的数组中为我提供了所有名称和所有 sections.description 我想要的是它应该一一给我。
例如第一个结果集应该在下面给我
[
"John"
]
and
[
"John smith"
"remain smith of the first object"
]
这样我就可以 运行 我现有的逻辑来检查 John 是否有空。目前我的逻辑 运行s 在描述的第一个条目上并且它不检查下一个条目或 section.description 这是下面匹配对象失败的原因,因为 "john" 出现在第二个条目中描述
{
"items": [
{
"id":11,
"name": "SMITH",
"sections": [
{
"id":11,
"description": "SMITH"
},
{
"id":11,
"description": "JOHN Carter"
}
]
}
]
}
我目前使用的匹配逻辑是-
* def matchText =
"""
function (nameArr, sectionArr, matchingWord)
{
for(var i = 0; i < nameArr.length; i++)
var regEx = new RegExp(matchingWord, 'gi')
var nameMatch = nameArr[i].match(regEx)
var secMatch = sectionArr[i].match(regEx)
if (nameMatch ==null && secMatch == null) {
return false;
}
}
return true;
}
"""
* def getName = get response.items[*].name
* def getDescription = get response.items[*].sections[*].description
* assert matchText(getName,getDescription,'john')
因此,当您的名称长度相同时,此逻辑有效 & sections.description 但是当 sections.description 有多个数组时,它不会正确迭代。这是我想将名称视为一个对象而将 sections.description 视为另一个对象的唯一原因,即使其中会有多个子 ID。
示例代码:
Feature: Validation
Scenario:
* def resp =
"""
{
"items": [
{
"id": 11,
"name": "JOHN",
"sections": [
{
"id": 11,
"description": "SMITH"
},
{
"id": 11,
"description": "JOHN Carter"
}
]
}
]
}
"""
* def names = []
* def fun =
"""
function(x){
var json = x.sections;
var temp = x.name
for(var i = 0; i < json.length; i++) {
var obj = json[i];
temp = temp + "," + obj.description
}
karate.appendTo(names, temp);
}
"""
* def items = get resp.items[*]
* karate.forEach(items, fun)
* match each names == '#regex .*JOHN.*'