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
Related
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
Multiple URL i am going to pass in background for each url path should run scenario
Background:
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Background:
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Scenario: Creates the webservices api page
Given path 'rest/activescript/about'
When method get
Then status 200
* def B = response
* print B
Actual Result : is last url in background will consider in scenario to execute
expected Result : all Url in background should run with diff scenario
The following should work. Though I'm not sure it is recommended to do this. The url is usually defined in karate-config.js for the whole run.
Scenario Outline: Scenario called multiple times
* url '<newUrl>'
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Given path 'rest/activescript/about'
When method get
Then status 200
* def B = response
* print B
Examples:
|newUrl |
|URL1|
|URL2 |
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' });
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
Scenario: Verify that Authentication is done or not
Given url '***********'
Given path 'authenticate'
And form field username = 'admin_cs'
And form field password = '********'
When method post
Then status 200
And header tokenn = response.token
* def accessToken = response.token
* print accessToken
Scenario: Verify Get all Clients
Given url '************'
Given path 'users/usersAssignable'
* header x-auth-token = accessToken
When method get
Then status 200
* def response = response
* print response
Please combine the two Scenario-s into one. Or move the first one here into the Background. Please read this very carefully: https://github.com/intuit/karate#script-structure