Karate Api Testing - How to pass data from one feature file to another [duplicate] - api

This question already has an answer here:
Calling common scenario from another scenario in same feature file where request body is store in json file in karate [duplicate]
(1 answer)
Closed 1 year ago.
I need to pass data from one feature file to another.
Feature(1): Create a new user
Background:
* url 'http://127.0.0.1:8900/'
* header Accept = 'application/json'
Scenario: Create a new user
Given path '/user'
And request {"email" : "test#test.com", "name" : "Brian"}
When method post
And def newUser = $..id
Then status 201
Feature(2): Call newUser from feature 1
Background:
* url 'http://127.0.0.1:8900/'
* header Accept = 'application/json'
Scenario: Call User
* def newUser = $..id
* print newUser

Please read the docs: https://github.com/intuit/karate#calling-other-feature-files
* def aVariable = "can be anything"
* def result = call read('one.feature') { some: 'data', useExpression: #(aVariable) }
And in one.feature you can get access to the JSON "argument"
* print some
Which should print the value data

Related

Retrieve a JWT from a feature A in a feature B

I’m trying to retrieve a JWT from a feature A in a feature B.
For this I have in the feature A:
# create API access for the client
Given url baseUrl
And path 'admin', 'clients', clientApiId, 'accesses', 'api', 'api-key', 'renew'
And header Authorization = 'Bearer ' + authenticationJWT
When method put
Then status 200
* def clientApiJWT = response
And in feature B:
# Create a process with API access
* def clientApiAccess = call read('classpath:karate/common/create-client-api-access.feature')
* clientApiJWT = clientApiAccess.clientApiJWT
With this code I recover the following error:
Thank you for your help
Shouldn't it be:
* def clientApiJWT = clientApiAccess.clientApiJWT

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

Polling in Karate based on JSON response [duplicate]

This question already has an answer here:
Karate framework retry until not working as expected
(1 answer)
Closed 2 years ago.
I am creating an animal into a database and then attempting to retrieve the animal which I have just created. However, there is a time lag ~5-10secs in the database. Therefore, a sleep wait is not suitable for this scenario as the response time varies. 
I would like to poll the message until animalId is returned in the array. It is also important that the requestId header is re-generated when the request is re-tried. 
What is the most elegant way of achieving this? 
Scenario:
Given path '/animals'
And header requestId = uniqueString(5)
When method post
Then status 200
* def animalId = response.animalId
Given path '/animals'
And header requestId = uniqueString(5)
When method get
Then status 200
{
"animals": [
{
"animalId": "12219958",
"reference": [
"12345"
]
}
]
}
* def animalDetails = karate.jsonPath (response, "$.animals.[?(#.reference[0]== '" + animalId + "' )]")[0]
* def animalId = '12345'
The retry until syntax is what you are looking for: https://github.com/intuit/karate#retry-until
EDIT: regarding this requirement:
It is also important that the requestId header is re-generated when the request is re-tried.
You have to configure headers with a JavaScript function, so that it is fired for each re-try. Otherwise there is no-other option to tweak the body, please manually write a polling loop if needed (also explained in the docs).
EDIT: here is a simple example to see for yourself how the foo header is dynamic for each re-try. Paste this in a fresh Scenario and it will work.
* def counter = { value: 0 }
* configure headers = function(){ return { foo: counter.value } }
* url 'http://httpbin.org/get'
* retry until counter.value++ == 2
* method get

pass parameters to after-feature karate

I discovered after-feature in karate which is very useful. But I didn't find how to pass parameters to after-feature from main feature. Ex: access token to delete a user account or a user_id.
Here is call of after-feature.feature in my main feature:
* configure afterFeature = function(){ karate.call('classpath: AfterFeature.feature'); }
Here is my AfterFeature.feature
Scenario:
* url 'XXX'
* path 'YYY'
* param foo = bar which should come from main feature
* header Authorization = 'Bearer ' + accessToken which should come from main feature
* method delete
* status 204
karate.call() can take parameters.
karate.call('classpath: AfterFeature.feature', { some: 'value' });

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

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