Home > Back-end >  Karate - how to use param from response to test child endpoints
Karate - how to use param from response to test child endpoints

Time:10-15

I have a parent URL that returns an array of objects with id and slug, this is dynamically generated because it depends of the user (test env). After getting this data, how can I dynamically call each element object from an array and make another request ?

Example :

Scenario: parent
  Given url 'posts'
  Then Status 200
  * assert response.status == true

Example url/posts returns a data of array of objects :

[
  {id: 6, slug: 'my-post-6'},
  {id: 23, slug: 'example-test-23'},
  {id: 133, slug: 'another-test-133'},
]

Then I want to make a get request to every object like so : url 'posts/' data.slug

How can I do this ?

GET url 'posts/my-post-6'
// Validate schema
GET url 'posts/example-test-23' 
// Validate schema
GET url 'posts/another-test-133' 
// Validate schema

CodePudding user response:

Karate's call keyword auto-loops over a JSON array of objects. Refer the docs: https://github.com/karatelabs/karate#data-driven-features

You can try this simple example:

Feature:

Scenario:
* def data = [{ slug: 'one'}, { slug: 'two'}]
* call read('@called') data

@called @ignore
Scenario:
* url 'https://httpbin.org/anything'
* path slug
* method get

This is getting improved in future versions, refer: https://github.com/karatelabs/karate/issues/1905 (available now in 1.3.0.RC2)

  • Related