vdrulerz
vdrulerz

Reputation: 264

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

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

Upvotes: 1

Views: 934

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

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.

Upvotes: 1

Related Questions