Need a way to set global headers(this headers are returned by feature file) for all api requests [closed]
Need a way to set global headers(this headers are returned by feature file) for all api requests [closed]
We need to pass cxrf token, cookie returned from login feature file to all the API requests.
For now, I am passing these cxrf token and cookie for each request by passing parameters to calling feature files.
Is there any way to set these values globally.

Calling feature:
@userManagement
Feature: Access Profile creation, updating, and deletion Test
Background:
* def result = callonce read('classpath:ic/common/resources/LoginFinalSub.feature')
* def csrfToken = result.response.csrfToken
* def jsessionid = result.responseCookies.JSESSIONID.value
* def appServer = result.requestUri
* def routevalue = result.responseCookies.route.value
Scenario: create User
Given url appServer
* print 'create user'
* def result = call read('classpath:ic/common/UsersManagement/createUser.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid,route: routevalue, inputFile: 'ic/common/UsersManagement/createUser.json'
Then match result.responseStatus contains 201
Then match result.response.integration.message.code contains 0
Then match result.response.integration.message.description contains 'Success'
Scenario: update user details and assert updated user details
Given url appServer
#fetch user
* def result = call read('classpath:ic/common/UsersManagement/getUserID.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid, route: routevalue, userToDelete: 'testuser'
Then match result.responseStatus contains 200
* def uid = result.user_id
* print uid
# update user details
* def result = call read('classpath:ic/common/UsersManagement/updateUser.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid, route: routevalue, userId: uid, inputFile: 'ic/common/UsersManagement/updateUserInput.json'
Then match result.responseStatus contains 200
Then match result.response.integration.message.code contains 0
#validate updated user details
* def result = call read('classpath:ic/common/UsersManagement/getUserDetails.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid, route: routevalue, userId: uid
Then match result.responseStatus contains 200
Then match result.response.integration.message.code contains 0
Then match result.response.integration.serviceData.user.id contains uid
Then match result.response.integration.serviceData.user.first_name contains 'testFirstUpdated'
Then match result.response.integration.serviceData.user.last_name contains 'testLastUpdated'
Then match result.response.integration.serviceData.user.title contains 'testUpdated'
Then match result.response.integration.serviceData.user.employee_number contains 'testUpdated'
Then match result.response.integration.serviceData.user.email contains 'testUpdated@test.com'
Then match result.response.integration.serviceData.user.username contains 'testuserUpdated'
Then match result.response.integration.serviceData.user.username contains 'testuserUpdated'
Then match result.response.integration.serviceData.user.federation_id contains 'Updated'
Then match result.response.integration.serviceData.user.accessProfileId.displayValue contains 'Regular User'
Scenario: fetch User and delete the user
Given url appServer
* def result = call read('classpath:ic/common/UsersManagement/getUserID.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid, route: routevalue, userToDelete: 'testuserUpdated'
Then match result.responseStatus contains 200
* def uid = result.user_id
* def result = call read('classpath:ic/common/UsersManagement/deleteUser.feature') X-CSRF-TOKEN: csrfToken, JSESSIONID: jsessionid, route: routevalue, userId: uid
Then match result.responseStatus contains 200
Then match result.response.integration.message.code contains 0
Then match result.response.integration.message.description contains 'Success
Called features examples:
Feature: create Access Profile
Background:
Scenario: create Access Profile
Given url appServer
And path '/integration/rest/user'
And header Accept = 'application/json'
And header Content-Type = 'application/json'
And header X-CSRF-TOKEN = csrfToken
* cookie JSESSIONID = jsessionid
* cookie route = routevalue
* def inputjson = read('classpath:'+inputFile)
* print inputjson
Given request inputjson
When method post
Need a way to make cxrf token and cookie headers as global for all called features
This question appears to be off-topic. The users who voted to close gave this specific reason:
I have updated the code sample for your reference. Please have a look
– Anupama
Sep 17 '18 at 15:54
1 Answer
1
Refer this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/headers/call-updates-config.feature
Especially this part:
Background:
* callonce read('common.feature')
Now the variables in common.feature will be global. This will solve your problem.
common.feature
Please please please read the documentation here: https://github.com/intuit/karate#shared-scope
Hi Peter, This solution works perfectly fine. But I am facing issue to configure heades in js file. my headers are returned from common.feature. I need to put them in js file and call that js file in called feature. my headers include X-CSRF-TOKEN,JSESSIONID. how to set values for heades in js file so that it can be used for called feature. waiting for response. Thanks in advance
– Anupama
Sep 18 '18 at 1:50
@Anupama please. i know reading docs is hard for you, but please try harder. i am busy today
– Peter Thomas
Sep 18 '18 at 1:52
Never post pictures of text when you can post the actual text. Be sure that it is properly formatted and readable.
– user177800
Sep 17 '18 at 15:44