1

I need to run all scenarios in a feature file against multiple urls (endpoints) one endpoint at a time. Basically I have a list of servers that's injected into the karate config object as an array. I call them in several feature files where I trigger all the scenarios against one URL (string) from the server list (array) at a time.

For instance, we have 1 feature file with 2 scenarios and a server list with 5 endpoint URLs. (FYI - we will be triggering several feature files in reality)

We set the url to first endpoint from the server list (array) and trigger the 2 scenarios with that URL only. Then repeat for the next 4 endpoints. Are we making 2 HTTP request per endpoint since we have 2 scenarios or 1 HTTP request per endpoint?

I referenced the Dynamic Scenario Outline example from the Karate docs. The JSON array data source example actually triggered all the scenarios with each URL one by one. However, I need to use the Examples table for another set of data for each scenario. This capability is triggered when the table consists of a single "cell", i.e. there is exactly one row and one column in the table.

I tried using @setup but I need to upgrade my Karate to 1.x and above ((I'm using 0.9.3) to find out if that would work. However, I have been running in circles with this. Is there an easier way to resolve this? Am I on the right track? Will using @setup be a good option to achieve what I described? Or should I look into using Background?

Context:

serverList defined in karate.config.js --> ['www.cat.com', 'www.dog.com', 'www.horse.com'] // an array with string elements (urls)

Feature: Run all the scenarios against each endpoint url

@setup
Scenario: Trigger all the scenarios with ONE endpoint url at a time
  * def endpointObj = karate.mapWithKey(serverList, 'endpointUrl')

 Scenario Outline: Print endpoint
  * def baseUrl = '<endpointUrl>'
 Examples:
  | karate.setup().endpointObj |

Scenario: First scenario

 * url baseUrl // I feel like i'm messing up here. Will this work?
 * print baseUrl // 1. 'www.cat.com', 2. 'www.dog.com' 3. 'www.horse.com' 4..5

Scenario: Second scenario

 * url baseUrl // I feel like i'm messing up here. Will this work?
 * print baseUrl // 1. 'www.cat.com', 2. 'www.dog.com' 3. 'www.horse.com' 4..5


Expected Outcome: trigger all scenarios against each endpoint URL from the server list.

If there are 2 scenarios and 5 endpoints in 1 feature file, there will be a total of 10 tests.

halfer
  • 19,824
  • 17
  • 99
  • 186
renc17
  • 31
  • 3
  • sorry I tried to re-read this a few times and gave up. see if this example helps: https://stackoverflow.com/a/75027059/143475 - and maybe this will give you the solution (hint: split into 2 feature files): https://stackoverflow.com/a/60387907/143475 – Peter Thomas Feb 22 '23 at 02:42

1 Answers1

1

Don't force yourself into Background or @setup. There are other ways to loop.

Here are 2 suggestions:

  1. call a second feature: https://stackoverflow.com/a/60387907/143475

  2. use the equivalent of an inner loop. In old versions of karate, use the full feature name in the read() argument:


Feature:

Scenario:
* def d1 = [{a: 1}, {a: 2}, {a: 3}]
* call read('@one') d1

@ignore @one
Scenario: one
* def d2 = [{b: 1}, {b: 2}, {b: 3}]
* call read('@two') d2

@ignore @two
Scenario: two
* print 'a:', a, 'b:', b
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248