简体   繁体   中英

Best Way to Iterate Through JSON Object Passed to Feature File in Karate

I have a JSON object like {"id1": 123, "id2": 234} , that I pass to a feature file in order to plugin each of the values above for a series of API requests like:

And path `/somePath/${id1}/detail`

And request {"id": "#(id1)", "action": "Reassign"}

My first thought was to use a Scenario Outline , but couldn't figure out how to set the ID's in the Examples table so they were properly read. Then I looked in documentation, and it seems like when I pass a JSON object like this, it should run the scenario for each value automatically. The problem is, not sure how to set the variable, as the key changes each time. Or maybe there is a much better way to do this I'm not seeing?

In order for it to work your keys need to be the same. In this case you actually have a single JSON object with two properties (id1, id2). Instead what it sounds like you want are two objects:

[
    { "id": 123, "path": "/mydir1/" },
    { "id": 456, "path": "/mydir2/" }
]

Also notice that it has to be a list/array of json objects that you are passing in, not just one blob like you have. And if each object contains a path name, you should add that internal to the object itself.

But, again, notice that each object in the list/array has the same structure, just with different data. You then use the common property names in your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM