How to pass the json list response of one feature file as parameter to another feature file - karate

My requirement is, I want to pass the response of first feature file as input to second feature file. The first feature file response is a json list, so the expectation is second feature file should be called for each value of json list.
Feature:
Scenario: identify the reference account
* def initTestData = read('../inputData.feature')
* def accountData = $initTestData.response
* print “Account Details”+accountData // output of this is a json list [“SB987658”,”SB984345”]
* def reqRes = karate.call('../Request.feature', { accountData : accountData })
In Request.feature file we are constructing the url dynamically
Given url BaseUrl + '/account/'+'#(accountId)' - here am facing issue http://10.34.145.126/account/[“SB987658”,”SB984345”]
My requirement is Request.feature should be called for each value in ‘accountData’ Json list
I have tried:
* def resAccountList = karate.map(accountData, function(x){accountId.add(x) })
* def testcaseDetails = call read('../requests/scenarios.feature') resAccountList.accountId
Result is same, accountId got replaced as [“SB987658”,”SB984345”]
my I need to call Request.feature twice http://10.34.145.126/account/SB987658 http://10.34.145.126/account/SB984345 and use the response of each call to the subsequent feature file calls.

I think you have a mistake in karate.map() look at the below example:
* def array = ['SB987658', 'SB984345']
* def data = karate.map(array, function(x){ return { value: x } })
* def result = call read('called.feature') data
And called.feature is:
Feature:
Scenario:
Given url 'https://httpbin.org'
And path 'anything', value
When method get
Then status 200
Which makes 2 requests:
https://httpbin.org/anything/SB987658
https://httpbin.org/anything/SB984345

Related

How to traverse through a Json in Karate

I am trying to have a Karate test where I need to traverse through a json to get the required test data.
I have the below json.
{
"dev":{
"scenario1":{
"data":"data1"
},
"scenario2":{
"data":"data2"
}
},
"qa":{
"scenario1":{
"data":"data1"
},
"scenario2":{
"data":"data2"
}
}
}
Below is my feature file.
Background:
* def env = dev
Scenario:
* Given url someurl
* def inputData = testdata.env.scenario1.data
* And request { input: '#(inputData)'}
* When method post
* Then status 200
I need the test data that matches the value env that I have defined above.
How can I set the json path to my input data. Basically, the json path should take the value from a variable I have defined previously.
For this you don't even need Json-Path, just JS will do:
* def data = { foo: 1, bar: 2 }
* def env = 'bar'
* def res = data[env]
* match res == 2
Also see: https://stackoverflow.com/a/59162760/143475
You can also do dynamic Json-Path, see: https://stackoverflow.com/a/50855425/143475

In Karate - Feature file calling from another feature file along with variable value

My apologies it seems repetitive question but it is really troubling me.
I am trying to call one feature file from another feature file along with variable values. and it is not working at all.
Below is the structure I am using.
my request json having variable name. Filename:InputRequest.json
{
"transaction" : "123",
"transactionDateTime" : "#(sTransDateTime)"
}
my featurefile1 : ABC.Feature
Background:
* def envValue = env
* def config = { username: '#(dbUserName)', password: '#(dbPassword)', url: '#(dbJDBCUrl)', driverClassName: "oracle.jdbc.driver.OracleDriver"};
* def dbUtils = Java.type('Common.DbUtils')
* def request1= read(karate.properties['user.dir'] + 'InputRequest.json')
* def endpoint= '/v1/ABC'
* def appDb = new dbUtils(config);
Scenario: ABC call
* configure cookies = null
Given url endpoint
And request request1
When method Post
Then status 200
Feature file from which I am calling ABC.Feature
#tag1
**my featurefile1: XYZ.Feature**
`Background`:
* def envValue = env
Scenario: XYZ call
* def sTransDateTime = function() { var SimpleDateFormat = Java.type('java.text.SimpleDateFormat'); var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+00:00'"); return sdf.format(new java.util.Date()); }
* def result = call read(karate.properties['user.dir'] + 'ABC.feature') { sTransDateTime: sTransDateTime }
Problem is,
While executing it, runnerTest has tag1 configured to execute.
Currently, it is ignoring entire ABC.feature to execute and also not generating cucumber report.
If I mention the same tag for ABC.feature (Which is not expected for me as this is just reusable component for me ) then it is being executed but sTransDateTime value is not being passed from XYZ.feature to ABC.feature. Eventually, InputRequest.json should have that value while communicating with the server as a part of the request.
I am using 0.9.4 Karate version. Any help please.
Change to this:
{ sTransDateTime: '#(sTransDateTime)' }
And read this explanation: https://github.com/intuit/karate#call-vs-read
I'm sorry the other part doesn't make sense and shouldn't happen, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Karate: How can we retrieve the value from called feature file

I have two parameter in feature file A and I pass those values to another feature file called B.
But I am unable to retrieve the values as expected in Feature file B
CODE:
Feature File A:
And def Response = response
And def token = response.metaData.paging.token
And def totalPages = response.metaData.paging.totalPages
* def xyz =
"""
function(times){
for(currentPage=1;currentPage<=times;currentPage++){
karate.log('Run test round: '+(currentPage));
karate.call('ABC.feature', {getToken:token, page:currentPage});
}
java.lang.Thread.sleep(1*1000);
}
"""
* call xyz totalPages
Feature File B:
* def token = '#(getToken)'
* def currentPage = '#(page)'
But the output was
#getToken
#page
What would be the best way? to these values for further utilization.
Try this:
* def token = getToken
* def currentPage = page
Here's another thing, any variable defined in the calling feature will be visible, e.g. token so most of the time you don't need to pass arguments:
* print token
* print totalPages
Please avoid JS for loops as far as possible: https://github.com/intuit/karate#loops - and actually you seem to have missed the data-driven testing approach that Karate recommends: https://github.com/intuit/karate#the-karate-way
If you are still stuck, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

how to pass a string in parm request and loop it in my request path without saving to a file and creating table

so i created/def a object with
And def memeberIDs = $.users[*].id
And print memeberIDs
and i get a list like so
[
"eOVbGI0XSiabeiLZtB-ROQ",
"iK-Fz_NRSbSt-7AdodjDrA",
"dS2czwVFRZmy8a6oxO7JKw",
"yxH3bmF3THuCgLWLGh7eeQ",
"bqD41MgvSlC94EQH-r1H-Q",
"MrQihYjiR_WmWHA2-Y2cZQ",
"9N7uZdT_RTmePdJ5DMZ7sg",
"qo76PFcSS3-61FwgKCz6Ng",
"tjo3sdj3RMe8RoSBR3U_Ng",
"OuFxQR7KThCBPv2wHtKzhg",
"YmQKkv69Ts2yQ32LIjJN-g",
"A7kRauysQsCtle9G-bMV0g",
"zinHreHLTluUWdFzavssEA",
"UhZt8B_JSVaPBAJdIcPBsw",
"MtT_yR1iQqmDWMWlxXsWmQ",
"y2c9aK17Qouune_c_ZGlYQ",
"xGHOe7wxQru-NruIBPHL7Q",
"sTe8TADNTQ6nmg4UJeXiOQ",
"qC2FJUJeQJmOJPKkE_iehQ",
"4V0a93O8TCK-jjDujVVH-A",
"sk1tDsUmRYWUjyCMWFOqDw",
"ChmxrwHwTcS9I3u-RveBQA",
"ZL3uSx1oQbOc2c5qrFYRew",
"qjxFk-x9SVe-8XkzMbLrBw",
"7uIQVAXuSVe4tcWdv6S7MQ",
"V4mdzUj2SpyVpKlJfSdOEg",
"dU61-sNoQu6Q87hItkGcHw",
"enhFrWkfTDuaFqQCwPetAw",
"Txte_5FtTiaSfsc5k7-HmA",
"7tkMxEglS5qgx6bKObByKg",
"-uv0OuoNRlinnI4HkGsSSg",
"tLxWhEqSSiOcqyGJKsG6tQ",
"8tqnoZ1DQrq0DGzU_4OVmg",
"E9Rjy2euRbaKeaP1INxvGA",
]
My next request looks like
Given url 'https://api.someurl.com/v2/users//meetings?type=all&page_size=30&page_number=1'
Take some time and study this example. And read this part of the documentation: https://github.com/intuit/karate#dynamic-scenario-outline
Background:
* def list = ['foo', 'bar']
* def data = karate.mapWithKey(list, 'name')
Scenario Outline:
* url 'http://httpbin.org'
* path 'anything'
* param test = name
* method get
Examples:
| data |
This example will make 2 requests,
1) GET http://httpbin.org/anything?test=foo
2) GET http://httpbin.org/anything?test=bar

How to pass single param to separate feature file

I've tried below to send a param to separate feature file ( by following this example ) but, it is not working as expected.
project.feature
* def id = '55000000005021'
* def result = call read('delete_project.feature')
delete_project.feature
Given path 'project', '#(id)'
When method DELETE
Then status 200
Then match response.status == 'success'
Getting below exception
com.intuit.karate.exception.KarateException: projects.feature:48 -
delete_project.feature:11 - status code was: 404, expected: 200,
response time: 239, url: https://localhost:8080/project/%23(id)
response:
{"status":"failure","data":{"error_code":"INVALID_URL_PATTERN","message":"Please
check if the URL trying to access is a correct one"}} at ✽.* def
result = call read('delete_project.feature') (projects.feature:48)
One more doubt, How to iterate this by passing list of ids. I've multiple id in foo variable, I would like call delete_project.feature for each id availble in that foo variable.
* def foo = get response.data[*].id
I think you over-complicated the path params, change to:
Given path 'project', id
And read this part of the docs: https://github.com/intuit/karate#rules-for-embedded-expressions