We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
Can we call a scenario from one feature in another using karate?
I have a concern with this approach: Karate should allow re-usability from a feature file itself for a particular scenario. If I have such numerous cases then i will end up with a lot of feature files which would be holding duplicate scenarios just for the sake of getting called. –
Splurge
No. You need to extract that Scenario into a separate*.feature
file and then re-use it using the call
keyword.
EDIT: Karate 0.9.0 onwards will support being able to call by tag as follows:
* def result = call read('some.feature@tagname')
Thanks, @Peter Thomas. We're really enjoying using Karate. The initial idea was to have a feature with scenarii about authentification, which also includes a successful scenario. And we wanted to use it for other features, which need it (acces token returned by this successful scenario). So, can we put this successful scenario in JavaScript and have our authentification feature separated? What would you recommend? If so, could you give us some tips to how to do this scenario in JS? –
Cryptic
please please look at the demo examples for call and headers. then ask specific questions. –
Religiose
@nirind - specifically look at this example: github.com/intuit/karate/blob/master/karate-demo/src/test/java/… –
Religiose
To illustrate the answer from Karate-inventor Peter Thomas with an example:
Given a feature-file some.feature
with multiple scenarios tagged by a tag-decorator:
@tagname
Scenario: A, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `uri`
* def uri = responseHeaders['Location'][0]
@anotherTag
Scenario: X, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `createdResourceId`
* def createdResourceId = $.id
In another feature we can call a specific scenario from that feature by its tag, e.g. tagname
:
Scenario: B, another case reusing some results of A
* def result = call read('some.feature@tagname')
* print "given result of A is: $result"
Given path result.uri + '/update'
See also: demo of adding custom tags to scenarios
@Noah Overcash thanks for fixing the broken link! –
Counterclaim
© 2022 - 2024 — McMap. All rights reserved.