I have the following use case. The top level feature file (test1.feature) calls two other feature files (test2.feature, test3.feature) like so:
test1.feature:
Feature:
Background:
* print 'this is a background 1'
Scenario: test1
* print 'this is test1'
* call read('test2.feature')
* call read('test3.feature')
test2.feature:
Feature:
Background:
* print 'this is a background 2'
* def testId = 222
* configure afterScenario = function(){ karate.write({ id: testId, errorMessage: karate.info.errorMessage }, 'test-id-' + testId + '.json'); }
Scenario: test2
* print 'this is test2'
test3.feature:
Feature:
Background:
* print 'this is a background 3'
* def testId = 333
* configure afterScenario = function(){ karate.write({ id: testId, errorMessage: karate.info.errorMessage }, 'test-id-' + testId + '.json'); }
Scenario: test3
* print 'this is test3'
I realize that writing to a file is not recommended, but in this particular case, I need to capture the test result for post execution processing (long story!). I'm also aware that the Background
gets loaded for every Feature/Scenario
. So once the calling feature (test1.feature) calls the first called feature (test2.feature), its Background
takes over. And this is where I'd like the afterScenario
hook from the called feature to be invoked, but since the calling feature (test1.feature) has not finished, it then proceeds to call the last feature (test3.feature). Similarly the Background
from the the last called feature (test3.feature) gets loaded and since the calling feature (test1.feature) is now done, it then invokes the afterScenario
hook from the last called feature (test3.feature), and as a result only test-id-333.json
file gets written to the target
folder. Here's the print output:
23:53:49.613 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is a background 1
23:53:49.617 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is test1
23:53:49.631 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is a background 2
23:53:49.637 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is test2
23:53:49.652 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is a background 3
23:53:49.654 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] this is test3
I know that this is the expected behavior, but was wondering if there is a way to invoke the afterScenario
hook in the called feature (test2.feature), even if the calling feature (test1.feature) has still work to do.
If I turn the test1.feature into a Scenario Outline
, then both files test-id-222.json
and test-id-333.json
get written, since now they're each separate scenario, but unfortunately this option is not viable in this use case.
It'd be nice if there was a afterThisScenario
, or a parameter to pass to a called feature i.e. * call read('test2.feature') withAfterHook
, that would ensure the after hook gets invoked, even if the calling feature is still not finished.
Thanks in advance for any suggestions!