Karate - Iterate through array and call rest api for each value

Viewed 35

Scenario:

  * def Test_assignment_type = 'ALL'
  * def TEST_NODE_ID = '123455667'

  * def list = [ '123', '1234', '12345' ]
  * def gId_list = karate.mapWithKey(list, 'HOOK_TEST_LOCATION_GID')  

  * callonce read('classpath:hook/delete-assignments-hook.feature') {HOOK_TEST_LOCATION_ID: "#(TEST_NODE_ID)", HOOK_TEST_ASSIGNMENT_TYPE: "#(Test_assignment_type)"}

delete-assignments-hook.feature:

Scenario:

  * Given path '/nodes/'+HOOK_TEST_LOCATION_ID+'/locations/'+HOOK_TEST_LOCATION_GID+'/assignments'
  * And param assignmentType = HOOK_TEST_ASSIGNMENT_TYPE
  * When method delete
  * Then assert responseStatus == 204  || responseStatus == 404

how to pass gId_list in my delete-assignments-hook.feature, so that delete api runs for each value in the list.

1 Answers

Just start with a simple example and study it. This below will make 3 calls with a parameter called id. And also read the documentation: https://github.com/karatelabs/karate#data-driven-features

Feature:

Scenario:
    * def array = [1, 2, 3]
    * def data = karate.mapWithKey(array, 'id')
    * call read('@called') data

@ignore @called
Scenario:
    * url 'https://httpbin.org/anything'
    * param id = id
    * method get
Related