Here is my feature file , which just loads the json file and wants to iterate over the same
Background:
* def kittens = read('../json/test.json')
Scenario Outline: cat name: <name>
* print <name>
Examples:
| name |
| kittens |
Here is the output
[
{
"name": "Bob"
},
{
"name": "Wild"
},
{
"name": "Nyan"
},
{
"name": "Keyboard"
},
{
"name": "LOL"
},
{
"name": "Ceiling"
}
]
As per my understanding this should run 7 times and give me individual variable values , But its running only once and giving me full json as output .
Let me know if I am missing anything.
You are passing the list/array with a variable name in it, it will run only once as it interprets your entire json data as single variable name.
you could have noted it printed the entire data in your test.json
once, as it acted as normal scenario outline.
You should pass the array directly as below to make it as dynamic scenario outline.
Feature: Dynamic Scenario Outline
Background:
* def kittens = read('../json/test.json')
Scenario Outline: cat name: <name>
* print <name>
Examples:
| kittens |
For dynamic scenario outline, the variables <name> will actually derived from your json, if there is key in your json as "name". Not as the header of the list in Examples:.
Karate docs- Dynamic Scenario Outline
#Babu Sekaran. It was not priting cat names as used as above.
It was iterating number of times but not printing anything. Then i used * print '' means included quotes. then it started printing cat names.
Feature: Dynamic Scenario Outline
Background:
* def kittens = read('test.json')
Scenario Outline: cat name: <name>
* print '<name>'
Examples:
| kittens |
Related
I started my adventure with Karate a month ago. I have a simple GET test called getAllCars.feature showing a list of cars currently available:
[
{
"brandName": "BMW",
"id": 1,
"winterTires": false,
"modelName": "X5"
},
{
"brandName": "Opel",
"id": 34,
"winterTires": true,
"modelName": "Insignia"
},
{
"brandName": "Mercedes-Benz",
"id": 36,
"winterTires": true,
"modelName": "GLE Coupe"
},
{
"brandName": "Huydai",
"id": 251,
"winterTires": false,
"modelName": "i30"
}
]
I have to use each id as a parameter for the next feature file, the problem is, the list of cars is dynamic, ids don't repeat and I will have to use this list of ids for several other feature files. I managed to create a helper getCarIds.feature, which creates an array of objects "carId": "#number":
Feature: Get car IDs
Scenario: Get car IDs
* call read('classpath:x/automation/cars/getAllCars.feature')
* def carIds = $response[*].id
* def carFeeder = karate.mapWithKey(carIds, 'carId')
The following getCarParameters.feature has to iterate over the array from getCarIds.feature and pass each id as a parameter to get a response with performance parameters of each car and I don't know how to use each id separately as a parameter (keeping in mind that the list of ids is changing):
Feature: Get parameters of each car
Scenario: Get parameters for each car
* call read('classpath:x/automation/cars/getCarIds.feature')
Given url carUrl + '/carparameters'
And param carId =
When method GET
Then status 200
I managed to do it when passing the values from getCarIds.feature to getCarParameters.feature like described here by adding following line to getCarIds.feature:
* call read('classpath:x/automation/cars/getCarParameters.feature') carFeeder
but several other tests require car ids. I need getCarIds.feature to be reusable, so I would have to retrieve data from feature file, which creates the array with ids, instead of passing it to the GET feature and apparently it isn't so easy. Maybe my approach is completely wrong.
I think this is a valid case for karate.callSingle(): https://github.com/karatelabs/karate#karatecallsingle
So you can actually stick this in any feature and it is guaranteed to execute only once across your test suite. If the data is indeed something used by a majority of your test suite, you could even do this initialization in karate-config.js.
So this should work. First the reusable feature common.feature. Instead of the hard-coded response, you know how to make an actual HTTP request.
#ignore
Feature:
Scenario:
* def response =
"""
[
{
"brandName": "BMW",
"id": 1,
"winterTires": false,
"modelName": "X5"
},
{
"brandName": "Opel",
"id": 34,
"winterTires": true,
"modelName": "Insignia"
}
]
"""
* print 'getting car ids'
* def carIds = response.map(x => ({ id: x.id }))
Note the use of the JS map() function above, which I have started to recommend instead of JsonPath.
And here is a feature that uses the above. This uses the new #setup annotation that makes it easy to "loop": https://github.com/karatelabs/karate#setup
You can try this example quickly, and watch it make 2 requests using a param id from the loop.
Feature:
#setup
Scenario:
* def data = karate.callSingle('call-single-common.feature').carIds
Scenario Outline:
* url 'https://httpbin.org/get'
* param id = id
* method get
Examples:
| karate.setup().data |
There are other ways to loop, refer: https://github.com/karatelabs/karate#data-driven-features
Here I would like to clarify about creating dynamic example table for a dynamic JSON index size
My JSON looks like
Env - Dev - 2 servers
"response": {
"abc": [{
"status": "pass"
.
.
},
{
"status": "pass"
.
.
}
]
}
Env - Uat - 3 servers
{
"response": {
"abc": [{
"status": "pass"
},
{
"status": "pass"
},
{
"status": "pass"
}
]
}
}
My scenario outline looks like
Scenario Outline: validating .....
When def result = callonce read('featurefilename#tagname')
Then print result
And print <status>
And print ...
And match ....
Examples:
|result.response.abc|
Errors for the above:
1) * dynamic expression evaluation failed:result.response.abc
2) com.intuit.karate.karateExpresion: ---- javascript evaluation failed result.response.abc, ReferenceError:"result" is not defined in at line number 1
Note - If I move step 'When def result = callonce read('featurefilename#tagname') to background it's working as expected but I can't use background in my feature file due to other factors.
Thanks in advance
Instead of providing index in a table you can leverage Dynamic Scenario Outline feature in karate.
In this case you you can pass the variable as a input to Examples. If the JSON provided above is from variable result then,
Examples:
| result.response.abc |
Refer the docs for more insights.
I have json file as below.
{
book : {bookId : '<bookId>' ,
bookName : '<bookName>'
},
staff : {
sfattid : '<sfattid>',
name : '<name>'
},
libraryMember : {
libMembId : '<libMembId>',
name : '<libraryMember>'
}
}
I have below CVS
enter image description here
The CSV file which have headers as the above variable in json file .I want to use CSV file in scenario outline table and set values for the JSON file .Can you please suggest some code which automatic reads the json set the corresponding value for each row .
I was thinking below code should work but it is not working
Feature: scenario outline using a dynamic table
from a csv file
Scenario Outline: staffname name: <name>
* text jsonPayLoad = call read('readJson.feature')
Given url 'http://localhost:8089/'
And path 'returnBook'
And request jsonPayLoad
When method post
Then status 200
Then match karate.jsonPath(response,"$.status") == '<status>'
Examples:
| read('bookreturn.csv') |
The value of read('bookreturn.csv') should be an Array of JSON objects.
For example in this example: https://github.com/intuit/karate#dynamic-scenario-outline
Make this change:
Background:
* def kittens = [{name: 'Billie'}, {name: 'Wild'}]
I have a sample json file as below
{
book : {bookId : '<bookId>' ,
bookName : '<bookName>'
},
staff : {
sfattid : '<sfattid>',
name : '<name>'
},
libraryMember : {
libMembId : '<libMembId>',
name : '<libraryMember>'
}
}
I have a csv file with below information
I want to set the values for each variable from csv file and set the REST request 3 times during run time .
Feature: scenario outline using a dynamic table
from a csv file
Scenario Outline: staffname name: <name>
# When json payload = {book : {bookId : '<bookId>' , bookName : '<bookName>',},staff : {sfattid : '<sfattid>', name : '<name>'},libraryMember : { libMembId : '<libMembId>' ,name : '<libraryMember>'}}
When json payload = read("request.json")
Given url 'http://localhost:8089/'
And path 'returnBook'
And request payload
When method post
Then status 200
Then match karate.jsonPath(response,"$.status") == '<status>'
Examples:
| read('bookreturn.csv') |
I have wrote below code which works perfectly but in below case the same json payload is present in feature file which I want to keep in a text file .Please suggest some code .
Feature: scenario outline using a dynamic table
from a csv file
Scenario Outline: staffname name: <name>
# When json payload = {book : {bookId : '<bookId>' , bookName : '<bookName>',},staff : {sfattid : '<sfattid>', name : '<name>'},libraryMember : { libMembId : '<libMembId>' ,name : '<libraryMember>'}}
When json payload = read("request.json")
Given url 'http://localhost:8089/'
And path 'returnBook'
And request payload
When method post
Then status 200
Then match karate.jsonPath(response,"$.status") == '<status>'
Examples:
| read('bookreturn.csv') |
Sorry, you can't optimize this any further because for <name> to work it has to be within the feature file itself. Personally I think you are un-necessarily trying to over-engineer your tests. There is nothing wrong with what you have already.
If you really insist - here is the alternative, refer: https://github.com/intuit/karate#data-driven-features
* def books = read('bookreturn.csv')
* def result = call read('called.feature') books
But you will need to use 2 feature files. Each book in the loop can be used in embedded expressions. So you can read from a JSON file, and any embedded expressions in the file will work.
Just stick to what you have, seriously !
I am facing few issues while using relative path and mapping test data from a JSON file. I am having JSON POST request and a test data file in JSON format.
This is the test data I am using.
{
"name": "Test Data",
"description": "Information's mainly related with Users",
"testData": [
{
"Scenario1": {
"givenName": "Joseph",
"familyName": "George",
"addressType": "Current",
"lineOne": "BNRA-222, Kowdiar lane",
"cityName": "Trivandrum",
"countryID": "India",
"postcode": "695006"
}
},
{
"Scenario2": {
"givenName": "Sreenath",
"familyName": "Bhasi",
"addressType": "Current",
"lineOne": "HSE-123, Karyavatom",
"cityName": "Trivandrum",
"countryID": "India",
"postcode": "695552"
}
}
]
}
This is the feature file
Feature: Test using the Data from a JSON file
Background:
* def baseJsonRequest = read('../requests/jsonrequest.json')
* def baseData = read('../data/sampledata.json')
* def endPointURL = endPointURI + path
Scenario: A sample scenario to test the data parametrization
Given url endPointURL
And request baseJsonRequest
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.givenName = baseData.testData[*].Scenario1.givenName
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.familyName = baseData.testData[*].Scenario1.familyName
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.addressType = baseData.testData[*].Scenario1.addressType
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.lineOne = baseData.testData[*].Scenario1.lineOne
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.cityName = baseData.testData[*].Scenario1.cityName
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.countryID = baseData.testData[*].Scenario1.countryID
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.postcode = baseData.testData[*].Scenario1.postcode
My Questions are:
I am not able to give relative path on both sides. The relative path is returning me a json array. For eg I cannot use $..Scenario1.givenName, which makes me write longer paths.
To include this mapping on every scenario will be practically difficult. How can we implement a parameterized solution for that. What will better way? Can I invoke the data reading using a feature file and pass the informations to another feature. If that's possible then I need to parameterize . How to do that?
Or do I need to use a java class to read the JSON file?
Yes, the moment you have a wildcard in JsonPath, it returns an array. Anyway, 2 points that should help here straight away:
you can move repeating nested paths into a table-set
you can refer to a nested chunk of JSON by assigning to a variable
So this should be the way to go:
* def first = get[0] baseData.testData[*].Scenario1
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson
| path | value |
| familyName | first.familyName |
| residenceAddress.addressType | first.addressType |
I would try to not use wildcards as far as possible, for e.g.
* def first = $baseData.testData[0].Scenario1
Hope this helps !