Getting the scenario name of the parent calling scenario

Viewed 28

I have a feature file that has a scenario that is used to generate a token for our api. We have the ability to pass in a claim that we can used to log which test created the token. Without passing in the name to the called feature (and updating 3000 tests) how can we get the name of the calling feature so that we can log that in the token? We would like to be able to make the change in the token scenario to centralize the change. I can get the scenario name via karate.info.scenarioName but this gives me the called scenario name and not the parent calling scenario.

1 Answers

I think the best option is you to define an extra variable something like this in the calling feature:

* def caller = karate.feature.fileName

And make use of the principle that variables are always "visible" within called features. If you want to do a "safe" get, use karate.get()

* def whoCalledMe = karate.get('caller')

What you ask for is possible, but I'm reluctant to share this as these are like internal API-s not designed for use:

* def caller = karate.engine.runtime.caller.parentRuntime.scenario.feature

That will give you a reference to a Karate Java object com.intuit.karate.core.Feature.

The karate.scenario and karate.feature APIs are available in 1.0 onwards and karate.info is not recommended any more: https://github.com/karatelabs/karate/wiki/1.0-upgrade-guide#karateinfo-deprecated

Related