筛选空手道测试响应对象以获得子列表? [英] Filtering down a Karate test response object to get a sub-list?

查看:48
本文介绍了筛选空手道测试响应对象以获得子列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供此功能文件:

Feature: test

  Scenario: filter response
    * def response =
    """
    [ 
        {
            "a": "a",
            "b": "a",
            "c": "a",
        },
        {
            "d": "ab",
            "e": "ab",
            "f": "ab",
        },
        {
            "g": "ac",
            "h": "ac",
            "i": "ac",
        }
    ]
    """
    * match response[1] contains { e: 'ab' }

如何过滤response使其等于:

{
    "d": "ab",
    "e": "ab",
    "f": "ab",
}

有内置的方法吗?用与您可以使用Java流过滤列表相同的方式?

Is there a built-in way to do this? In the same way as you can filter a List using a Java stream?

推荐答案

示例代码:

Feature: test

  Scenario: filter response
    * def response =
    """
    [ 
        {
            "a": "a",
            "b": "a",
            "c": "a",
        },
        {
            "d": "ab",
            "e": "ab",
            "f": "ab",
        },
        {
            "g": "ac",
            "h": "ac",
            "i": "ac",
        }
    ]
    """
    * def filt = function(x){ return x.e == 'ab' }
    * def items = get response[*]
    * def res = karate.filter(items, filt)    
    * print res

这篇关于筛选空手道测试响应对象以获得子列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆