【问题标题】:Karate API framework is there any way to iterate through the child values空手道 API 框架有没有办法遍历子值
【发布时间】:2019-11-11 12:36:39
【问题描述】:

我的方法获得了 90 美分的成功,但是当响应在其中一个子键中有多个条目时,逻辑就会失败,我无法放置一个适用于所有情况的通用逻辑.

响应样本是

{
  "items": [
             {
              "id":1,
              "name": "John",
              "sections": [
                          { 
                           "id":1,
                           "description": "John smith" 
                          }
                      ]
           }
         ]
}

现在我的用例说您搜索 John 文本,然后 items 数组将包含许多对象,其 items.name 或 items.sections.description 应包含“John”关键字

我输入的匹配逻辑运行良好,因为我正在遍历 items[].name 和 items.sections[].description

主要挑战来自sections[*].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(section[*].description 的多个条目)

我面临的问题是当我迭代 items[].name & items[].sections[*].description

它给了我所有的名字和所有的sections.description在单独的数组中我想要的是它应该一个一个地给我。

例如第一个结果集应该在下面给我

[
"John"
]

and 

[
"John smith"
"remain smith of the first object" 
]

这样我就可以运行现有的逻辑来检查 John 是否可用。目前我的逻辑在描述的第一个条目上运行,它不检查下一个条目或 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')

因此,当您在 name 和 section.description 中具有相同长度但 section.description 有多个数组时,此逻辑有效,则它无法正确迭代。这是我想将名称视为一个对象而将sections.description 视为另一个对象的唯一原因,即使其中会有多个子ID。

【问题讨论】:

    标签: loops karate jsonpath


    【解决方案1】:

    示例代码:

    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.*'
    

    【讨论】:

    • 没有朋友.....看起来你完全理解我的要求。您不能总是依赖 items.name,因为有可能“John”甚至不会出现在名称中,而只会出现在 section.description 中的一个孩子身上。请再次检查我的查询。
    • 如果您分享了您的代码 sn-p,这将有助于理解。无论如何,再试一次(不确定这是否是你要找的):` * def arr = get resp.items[0].sections[].description * string str = arr+ "," + resp.items[ 0].name * 匹配 str == '#regex .*JOHN.' `
    • 嗨@Neodawn....我已经用示例自动化代码更新了这个问题。查询现在对您来说应该很清晰了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2022-12-11
    • 2010-11-20
    • 2010-09-14
    • 2016-01-16
    相关资源
    最近更新 更多