How to loop over array values in a karate feature

大兔子大兔子 提交于 2021-02-04 20:59:18

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/60261042/how-to-loop-over-array-values-in-a-karate-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!