Search code examples
loopsautomationbddkarate

How to loop over array values in a karate feature


I am trying to loop over array values in a karate feature file. In a Feature1.feature - Scenario1, I have some values in the array ["UUID1","UUID2","UUID3"] and I want to call another feature file (Feature2.feature) (having a code to call a DELETE endpoint) of a service

Feature2.feature:

 @ignore

Feature: Delete

  Background:

    * url baseUrl
    * headers {content-type:'application/json'}

  Scenario: Delete Test Assets

    Given headers {uid: '#(UId)', cid:'#(CId)'}
    And path 'type', Type, 'id', AssetId
    When method delete
    Then status 204

What approach should I use to Feature1.feature to call the Feature2.feature in a loop?


Solution

  • If you have an array of primitives, you need to convert it into an array of JSON objects before doing a "loop call". Refer to the docs for karate.mapWithKey(): https://github.com/intuit/karate#json-transforms

    So do this:

    * def data =  ["UUID1","UUID2","UUID3"]
    * def data = karate.mapWithKey(data, 'uid')
    * call read('second.feature') data
    

    And in second.feature:

    * headers { uid: '#(uid)' }
    

    Of course, read the docs for call if needed: https://github.com/intuit/karate#data-driven-features