如何使用Karate获取一个键的所有值

huangapple go评论66阅读模式
英文:

How to get all the values from a key using Karate

问题

A Karate beginner here

This is my Scenario Outline that I am trying to use

    Scenario Outline: API Should Support Sorting
        Given path 'mypathHere'
        And params {sortColumn: <sort>, isActive:1}
        When method Get
        Then status 200
        * def data = karate.jsonPath(response,"$..<sortColumn>")
        * def dataSorted = karate.sort(data)
        And match data == dataSorted

        Examples:
            | sort      | sortColumn |
            | name      | name       |
            | unitCode  | code       |
            | shortName | shortName  |


I have a response with an object that looks like this

[
{
"type": "unit",
"id": 1004,
"name": "Name 1",
"code": "Code Name 1",
"shortName": "Short Name 1"
},
{
"type": "unit",
"id": 615,
"name": "Name 2",
"code": "Code 2",
"shortName": "Short Name 2"
},
{
"type": "unit",
"id": 1071,
"name": "Name 3",
"code": "Code 3",
"shortName": "Short Name 3"
}
]

I need to get a list of all the values for one of the keys.

I know that i can use this approach
  • def data = karate.jsonPath(response,"$..name")

But I wonder if there's a **Karate way** that I can use?
英文:

A Karate beginner here

This is my Scenario Outline that I am trying to use

Scenario Outline: API Should Support Sorting
    Given path &#39;mypathHere&#39;
    And params {sortColumn: &lt;sort&gt;, isActive:1}
    When method Get
    Then status 200
    * def data = karate.jsonPath(response,&quot;$..&lt;sortColumn&gt;&quot;)
    * def dataSorted = karate.sort(data)
    And match data == dataSorted

    Examples:
        | sort      | sortColumn |
        | name      | name       |
        | unitCode  | code       |
        | shortName | shortName  |

I have a response with an object that looks like this

 [
        {
            &quot;type&quot;: &quot;unit&quot;,
            &quot;id&quot;: 1004,
            &quot;name&quot;: &quot;Name 1&quot;,
            &quot;code&quot;: &quot;Code Name 1&quot;,
            &quot;shortName&quot;: &quot;Short Name 1&quot;
        },
        {
            &quot;type&quot;: &quot;unit&quot;,
            &quot;id&quot;: 615,
            &quot;name&quot;: &quot;Name 2&quot;,
            &quot;code&quot;: &quot;Code 2&quot;,
            &quot;shortName&quot;: &quot;Short Name 2&quot;
        },
        {
            &quot;type&quot;: &quot;unit&quot;,
            &quot;id&quot;: 1071,
            &quot;name&quot;: &quot;Name 3&quot;,
            &quot;code&quot;: &quot;Code 3&quot;,
            &quot;shortName&quot;: &quot;Short Name 3&quot;

        }
]

I need to get a list of all the values for one of the keys.

I know that i can use this approach

* def data = karate.jsonPath(response,&quot;$..name&quot;)

But I wonder if there's a Karate way that I can use?

答案1

得分: 2

# 第一种解决方案
* def data = response.map((item) => item.<sortColumn>)
* 打印 data

# 第二种解决方案
* def data2 = $response[*].<sortColumn>
* 打印 data2

示例:
  | sort      | sortColumn |
  | name      | name       |
  | unitCode  | code       |
  | shortName | shortName  |
英文:

First solution

* def data = response.map((item) =&gt; item.&lt;sortColumn&gt;)
* print data

second solution

* def data2 = $response[*].&lt;sortColumn&gt;
* print data2

Examples:
  | sort      | sortColumn |
  | name      | name       |
  | unitCode  | code       |
  | shortName | shortName  |

答案2

得分: 2

我会争辩说,你正在按照"karate"的方式来做,因为你在示例中实际上使用了"karate"关键词来获取期望的值。你的方法没有问题。以下是另一种使用karate.map的方法:

  • 定义键 =
  • 定义值 = karate.map(response,函数(item) { return item[键] })
英文:

I'd argue you are doing it the "karate" way as you are literally using the karate key word in your example to get the expected values. There is nothing wrong with your approach. Here is another way to go about it using karate.map

* def key = &lt;sortColumn&gt;
* def values = karate.map(response, function(item) { return item[key] })

答案3

得分: 1

这是一个有趣的挑战。以下是我的解决方案:

* def result = {}
* def keys = ['id', 'name', 'code', 'shortName']
* keys.forEach(x => result[x] = response.map(y => y[x]))
* print result

它将给你一个包含了键和对应数值的单一对象。如果需要,你可以轻松地进行排序的组合。

英文:

That's a fun challenge. Here's my solution:

* def result = {}
* def keys = [&#39;id&#39;, &#39;name&#39;, &#39;code&#39;, &#39;shortName&#39;]
* keys.forEach(x =&gt; result[x] = response.map(y =&gt; y[x]))
* print result

It will give you a single object, with the keys and values combined. You should be able to easily combine sorting if needed.

huangapple
  • 本文由 发表于 2023年7月7日 01:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定