1

I'm running tests using the Karate framework. As a setup step, I need to create some entities using a REST API. I create them using callSIngle in karate-config.js:

    const result = karate.callSingle('classpath:path/to/createEntities.feature', config)

The feature has a Scenario Outline, defining various entities that need to be created. The REST API returns an ID for each entity that is created.

How can I save these IDs? I tried several solutions, for example define a variable in the Background section of the Scenario Outline - doesn't work as it's overwritten by each test and only its last value is returned.

Background
    * def ids = {}
.....
Scenario Outline:
....
    * set ids.<index> = response.id

In this example, the result will only have one value inside the ids map, for the last scenario.

Anakin001
  • 1,226
  • 2
  • 14
  • 30

1 Answers1

1

Yes a Scenario Outline is not designed to be able to accumulate results. You might be able to append to a JSON array, but I leave it to you to experiment.

One thing that may work is if you are into Java, you can append data into some singleton, refer: https://stackoverflow.com/a/54571844/143475

Otherwise I recommend you use a table, the example here is probably the best simple reference: https://github.com/karatelabs/karate#data-driven-features

So you can have a tabular set of data, drive a loop and get the results as an array, ready to return or do whatever.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks, Peter, I'm sure that table is what I'm looking for, I will try to move the code to using table and hopefully everything will work. – Anakin001 Nov 02 '21 at 09:01