Karate : How to refer or read variable in Examples section which is defined in Scenario Outline - karate

In karate framework, I am trying refer variable in Examples section which is defined in Scenario Outline. Below is the code snippet of feature file.
Scenario Outline:
* print __row
* def data = read('test.csv')
* def selected = 'TRUE'
* def fun = function(x){ return x.Status == selected }
* def filtered = karate.filter(data, fun)
* print filtered
Examples:
| filtered |
After I execute this, getting below error.
*js failed:
org.graalvm.polyglot.PolyglotException: ReferenceError: "filtered" is not defined*
Can any one please let me know how this can be achieved?

The Scenario Outline is the last thing to take control so you need to understand the flow. Please refer to this answer: https://stackoverflow.com/a/75155712/143475
Maybe you should get a normal Scenario Outline to work before trying advanced things. Take some time to read the documentation: https://github.com/karatelabs/karate#data-driven-tests

Related

Karate: Is there a way to have setup for each scenario outline

I am currently exploring karate framework. For scenario outline, trying to read a file in setup() , pass the data to Examples section and then use data in Scenario outline. Each scenario require data from different file: Ex: scenario 1 -> test1.csv scenario 2-> test2.csv. I have tried using setup() for each scenario outline as below but both scenarios reading data from first setup().
Is there a way to achieve this. Please let me know.
Feature: Test scenarios
#setup
Scenario:
* def data = read('test1.csv') # reading this file which is required in scenario outline - first scenario
Scenario Outline: first scenario
* print __row
Examples:
| karate.setup().data |
#setup
Scenario:
* def data = read('test2.csv') # reading this file which is required in scneario outline - second scenario
Scenario Outline: second scenario
* print __row
Examples:
| karate.setup().data |
Yes, read the documentation: https://github.com/karatelabs/karate#setup
You can give names to the #setup:
Feature:
#setup=myname
Scenario:
* def data = [{ a: 1 }, { a: 2}]
Scenario Outline:
* print __row
Examples:
| karate.setup('myname').data |
That said, my suggestion is don't over-complicate things when you start out with Karate. Just use separate feature files.

How to generate more than one random UUID and use in scenario outline examples in karate [duplicate]

This question already has an answer here:
How to use cucumber table when it is code driven
(1 answer)
Closed 1 year ago.
I am new to Karate API, pardon me for the mistakes if any.
I want to generate multiple random UUID and then use them in scenario outline examples
Example:
Background:
def UUID = function() {return java.util.UUID.randomUUID() + ''}
Scenario outline: to do post call
Given url 'http://localhost:8080'
def UID = UUID()
print UID
And request {CID:"", name :""}
When method POST
Then status 201
Examples:
|CID| name|
|UID1| james|
|UID2| rahul|
Here in above 'Examples' I wanted to use randomly generated UUID in data table of examples so that I can run multiple scenarios for UUID with one POST API call.
First question: How can I generate multiple random UUID ?
Second question: once multiple UUID gets generated how can i call in scenario outline examples and use them?
Can anyone suggest me on this?
Please try running the following simple example.
Feature:
Background:
* def uuid = function(){ return java.util.UUID.randomUUID() + '' }
Scenario Outline:
* url 'https://httpbin.org/anything'
* param foo = uuid()
* request { item: '#(item)' }
* method post
Examples:
| item |
| first |
| second |
It will make 2 requests, and each request will use a different param called "foo" and the URL will be like this:
https://httpbin.org/anything?foo=c1b6ab3d-5952-413b-827c-d9579a0a93b6
So it is simple. Think of the Examples: as like a "loop". Each time the Scenario Outline runs, we are calling the uuid() function again, which will return a different, random value.

Calling feature file within another feature file using tags [duplicate]

We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
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')
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

Fetching few elements using "$.[:2]" operator throws error in karate. might be a bug

Example:
Scenario: test
* def response =
"""
[
"YEN01",
"DP258",
"SA661",
"BT202",
"UR809"
]
"""
* def subset = response.[:2]
* print subset
I tried response..[:2] . and also tried with enclosing in ().
Let me know if any one got this working.
Just adding one character will fix your problem !
* def subset = $response.[:2]
Karate defaults to JavaScript and when you want JsonPath evaluation you need to give a little hint to Karate. This is explained in the docs: https://github.com/intuit/karate#get-short-cut

Can we call a scenario from one feature in another using karate?

We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
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')
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