Karate API framework how to match the response values with the table columns?

五迷三道 提交于 2021-02-08 07:21:44

问题


I have below API response sample

    {
  "items": [
             {
              "id":11,
              "name": "SMITH",
              "prefix": "SAM",
              "code": "SSO"
           },
          {
              "id":10,
              "name": "James",
              "prefix": "JAM",
              "code": "BBC"
          }
         ]
}

As per above response, my tests says that whenever I hit the API request the 11th ID would be of SMITH and 10th id would be JAMES

So what I thought to store this in a table and assert against the actual response

  * table person
          | id        | name       |
          | 11        | SMITH      |
          | 10        | James      |
          | 9         | RIO        |

Now how would I match one by one ? like first it parse the first ID and first name from the API response and match with the Tables first ID and tables first name

Please share any convenient way of doing it from KARATE


回答1:


There are a few possible ways, here is one:

* def lookup = { 11: 'SMITH', 10: 'James' }
* def items =
"""
[
   {
      "id":11,
      "name":"SMITH",
      "prefix":"SAM",
      "code":"SSO"
   },
   {
      "id":10,
      "name":"James",
      "prefix":"JAM",
      "code":"BBC"
   }
]
"""
* match each items contains { name: "#(lookup[_$.id+''])" }

And you already know how to use table instead of JSON.

Please read the docs and other stack-overflow answers to get more ideas.



来源:https://stackoverflow.com/questions/58992228/karate-api-framework-how-to-match-the-response-values-with-the-table-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!