Search code examples
karate

How can I loop over array of Ids then issue multiple DELETE operation?


I have a DELETE.feature file written like this

Scenario: DELETE api/v1/session/delete_session
Given path 'api/v1/session/delete_session'
And param session_id = _session_id
When method delete
Then status 200

Now I would like to call this feature file and pass the array of _session_id so we can do multiple deletion.

In the parent feature file, I have written a background process like this:

 Background:
 * def sessionIdList = ['abc','def']
 * def delete = call read('j_session_delete_session.feature' {_session_id: '#(sessionIdList)'}

The issue is Karate tries to issue a single DELETE option with multiple parameters e.g. DELETE .../api/v1/session/delete_session?session_id='abc'&session_id='def'

Question: How do I write the background task to enable multiple DELETE calls?

Note: The closest article I can find is in here but it doesn't quite answer my question :( Karate For loop to get ids based on pattern and then use a delete feature


Solution

  • I think you missed the part that the parameter to a call has to be an array of JSONs. Try this:

    * def sessionIdList = [{id:'abc'},{id:'def'}]
    

    And then:

    And param session_id = id
    

    There are many ways to turn an array of strings into an array of JSON objects, for e.g.

    * def data = sessionIdList.map(x => ({ id: x }))
    

    Read this for more tips: https://stackoverflow.com/a/78673358/143475