1

I need variables to be re-used (shared) across scenarios in the same feature file. Please find below the working way that I'm currently using.

The problem here is that I have to outsource the shared variables to another feature file what seems to be quite cumbersome for such a silly task.

I was wondering if I could define the re-usable variables as an ignored scenario in the same feature file that I can callonce from "myself" (the same feature file) as follows:

File my.feature:

Feature: My

Background:
* url myUrl
# call once explicitly the scenario tagged with '@init'
* def vars = callonce read('my.feature@init')

@ignore @init
Scenario: Return shared variables for all scenarios
    * def id = uuid()

# the non-ignored scenarios follow below this line...

Problem: Unfortunately this leads to an endless loop with many errors. It seems like callonce myself (the same file that invokes callonce) runs the Background including the callonce again.

Is the idea shown above possible and if yes, where's my mistake?

Or could you callonce without processing the Background again? Something like adding an argument to callonce or use karate.callSingle(file, dontProcessBackground=true)?

Many thanks.

--

The following works (but is cumbersome):

File my.feature:

Feature: My

Background:
* url myUrl
* def vars = callonce read('my.init.feature')

@one
Scenario: One
    * def payload = `{ "id" : "${vars.id}" }`
    * request payload
    * method post
    * status 200
    * match $.value == 'one'

@two
Scenario: Two
    * def payload = `{ "id" : "${vars.id}" }`
    * request payload
    * method post
    * status 200
    * match $.value == 'two'

File my.init.feature:

@ignore
Feature: Create variables to be used across mutliple scnearios

Scenario: Return shared variables for all scenarios
    * def id = uuid()

... where uuid() is shared in karate-config.js:

function fn() {
    var uuid = () => { return String(java.util.UUID.randomUUID().toString()) };
    // ...
    var config = { uuid: uuid }
    return config;
}
droptix
  • 53
  • 6

1 Answers1

0

I have to outsource the shared variables to another feature file

There is nothing wrong with using a second file for re-usable stuff. All programming languages work this way.

If this is such an inconvenience, kindly contribute code to Karate, it is an open-source project.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • You're right, in general there's nothing wrong with outsourcing code. It's just a bit inconvenient if you need *just one* shared variable -> then this just results in a poor readability. I was wondering if this is actually a bug? I'm not sure but shouldn't `callonce` cache the result? So even if you call the same feature file again, shouldn't this return the cached result instantly instead of processing the `callonce` in the `Background` again? – droptix Jul 06 '21 at 06:24
  • @droptix so my stand is that it is not a bug. so you are welcome to fix the code :) – Peter Thomas Jul 06 '21 at 06:39
  • Well, happy to do so. I checked the code and the error occurs between thread lock and release... I think it happens exactly here: https://github.com/intuit/karate/blob/master/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java#L1989 Maybe I really find something... give me some time to investigate – droptix Jul 06 '21 at 07:06
  • I opened an issue and added my code proposal here: https://github.com/intuit/karate/issues/1669 Unfortunately I'm not a Java programmer so I did what I could... but has been flagged as "wontfix" because it's not a priority. – droptix Jul 06 '21 at 08:52