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;
}