Reputation: 117
So I've managed to write a bunch of tests and in every feature file I set the same request headers.
For example:
Given url appUrl
And path '/path'
* header Accept = 'application/json'
I'd like to know if there's a way to set a header once so that it is set before each scenario is run. I've read the documentation and tried the callSingle method as follows in karate-config.js:
karate.callSingle('classpath:api/Utilities/Feature/header.feature');
header.feature looks like:
Feature: common routing that sets the headers for all features
Background:
* configure headers = { Accept : 'application/json' }
And example feature where I expect the headers to be preset:
Feature: Header Preset
Scenario: I expect the header to be set
Given url appUrl
And path '/path'
When method get
Then status 200
* print response
#I expect the response to be returned in JSON format
However I'm unable to get this working. I don't think I've understood how the callSingle method works. Some pointers would be helpful. Thanks.
Upvotes: 5
Views: 14675
Reputation: 181
For custom headers - in each feature file
* configure headers = read('file:src/test/java/common/set-headers.js')
set-headers.js -
function fn() {
return {
"header1": header1_value,
"header2": header2_value
};
}
Upvotes: 2
Reputation: 58058
Ignore callSingle
for now and focus on configure headers
.
I think you are missing one step - which is to ensure that configure headers
has been "applied" before each Scenario
. If you are 100% sure that this applies "globally", just do this in karate-config.js
:
karate.configure('headers', { Accept: 'application/json' });
Else you use the Background
(in each feature):
* configure headers = { Accept: 'application/json' }
Typically you have more steps that are common, so you have them in a "common" feature file and call
that for every test. Refer: https://github.com/intuit/karate#shared-scope
Upvotes: 4