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

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.

Related

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

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

In Scenario outline how to set the the limitation for reading the data from the csv files

Consider the below example
Feature: scenario outline using a dynamic table
from a csv file
Scenario Outline: cat name:
Given url demoBaseUrl
And path 'cats'
And request { name: '#(name)', age: '#(age)' }
When method post
Then status 200
And match response == { id: '#number', name: '#(name)' }
# the single cell can be any valid karate expression
# and even reference a variable defined in the Background
Examples:
| read('kittens.csv') |
If the above kittens.csv file contains 100 rows I want to read only 50 rows to execute the scenario outline .Is there any way in karate to ready only the given n rows from csv file
Since it is pure JS, any Array operation is possible. So you can easily do this:
| read('kittens.csv').slice(0, 49) |
That said, Karate was not designed for this. You may be better off trying to do data-driven tests like this: https://stackoverflow.com/a/72388475/143475

I am willing to compare and match each object sequentially in two JSONs and report mismatches in the report, can we do this natively in karate? [duplicate]

Is it possible to continue the execution of test step even if one of the assert/match fails?
Ex:
Scenario: Testing
* def detail = {"a":{"data":[{"message":["push","dash"]},{"message":["data","Test"]}]}}
* match detail contains {"a":{"data":[{"message":["push","dash"]}]}}
* print detail
Here match will fail but execution stop at that point.
is there a way to do a soft assertion so that next step gets executed?
EDIT in 2021 - a PR introducing a continueOnStepFailure flag was contributed by Joel Pramos here and is available in Karate 1.0 onwards. You can find more details here: https://stackoverflow.com/a/66733353/143475
If you use a Scenario Outline each "row" is executed even if one fails.
Scenario Outline: Testing
* def detail = { a: 1, b: 2, c: 3 }
* match detail contains <expected>
Examples:
| expected |
| { a: 1 } |
| { b: 2 } |
| { c: 3 } |
Note that the concept of "soft assertions" is controversial and some consider it a bad practice:
a) https://softwareengineering.stackexchange.com/q/7823
b) https://martinfowler.com/articles/nonDeterminism.html
For those looking for a way to show all mis-matches between 2 JSON objects, see this: https://stackoverflow.com/a/61349887/143475
And finally, since some people want to do "conditional" match logic in JS, see this answer also: https://stackoverflow.com/a/50350442/143475

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.

Karate: Match/Assert highlighting all the mismatching elements in one go while comparing 2 service call response [duplicate]

Is it possible to continue the execution of test step even if one of the assert/match fails?
Ex:
Scenario: Testing
* def detail = {"a":{"data":[{"message":["push","dash"]},{"message":["data","Test"]}]}}
* match detail contains {"a":{"data":[{"message":["push","dash"]}]}}
* print detail
Here match will fail but execution stop at that point.
is there a way to do a soft assertion so that next step gets executed?
EDIT in 2021 - a PR introducing a continueOnStepFailure flag was contributed by Joel Pramos here and is available in Karate 1.0 onwards. You can find more details here: https://stackoverflow.com/a/66733353/143475
If you use a Scenario Outline each "row" is executed even if one fails.
Scenario Outline: Testing
* def detail = { a: 1, b: 2, c: 3 }
* match detail contains <expected>
Examples:
| expected |
| { a: 1 } |
| { b: 2 } |
| { c: 3 } |
Note that the concept of "soft assertions" is controversial and some consider it a bad practice:
a) https://softwareengineering.stackexchange.com/q/7823
b) https://martinfowler.com/articles/nonDeterminism.html
For those looking for a way to show all mis-matches between 2 JSON objects, see this: https://stackoverflow.com/a/61349887/143475
And finally, since some people want to do "conditional" match logic in JS, see this answer also: https://stackoverflow.com/a/50350442/143475