空手道-设置全局请求标头 [英] Karate - Setting global request headers
问题描述
因此,我设法编写了一系列测试,并且在每个功能文件中都设置了相同的请求标头.
So I've managed to write a bunch of tests and in every feature file I set the same request headers.
例如:
Given url appUrl
And path '/path'
* header Accept = 'application/json'
我想知道是否有一种方法可以一次设置标题,以便在运行每种方案之前对其进行设置.我已经阅读了文档,并在karate-config.js中尝试了如下的callSingle方法:
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如下:
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
但是我无法正常工作.我认为我不了解callSingle方法的工作方式.一些指针会有所帮助.谢谢.
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.
推荐答案
Ignore callSingle
for now and focus on configure headers
.
我认为您缺少一个步骤-确保在每个 Scenario
之前,已应用"了 configure headers
.如果您100%确保此操作全局"适用,请在 karate-config.js
中执行此操作:
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' });
否则,您将使用 Background
(在每个功能中):
Else you use the Background
(in each feature):
* configure headers = { Accept: 'application/json' }
通常,您有更多常见的步骤,因此将它们放在通用"功能文件中,并针对每次测试进行 call
.请参阅: https://github.com/intuit/karate#shared-scope
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
这篇关于空手道-设置全局请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!