空手道 - 设置全局请求标题

发布于 2025-01-24 05:51:20 字数 878 浏览 1 评论 0 原文

因此,我设法编写了一堆测试,在每个功能文件中,我都设置了相同的请求标题。

例如:

Given url appUrl
And path '/path'
* header Accept = 'application/json'

我想知道是否有一种设置标头的方法,以便在运行每个方案之前设置它。我已经阅读了文档,并尝试了呼叫方法,如Akarate-config.js:

karate.callSingle('classpath:api/Utilities/Feature/header.feature');

header.feature看起来如下所示:

Feature: common routing that sets the headers for all features

  Background:
    * configure headers = { Accept : 'application/json' }

和示例功能,我希望标题可以预设:

 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 

但是我无法使此功能正常工作。我认为我不了解呼叫方法的工作方式。一些指针会有所帮助。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2025-01-31 05:51:20

忽略现在就呼叫,然后专注于 配置头< /代码>

我认为您缺少一个步骤 - 这是为了确保配置标头在每个方案之前已被“应用”。如果您100%确定此应用“全球”,则只需在 karate-config.js 中执行此操作:

karate.configure('headers', { Accept: 'application/json' });

否则,您使用 background> Background (在每个功能中):

* configure headers = { Accept: 'application/json' }

通常有更多常见的步骤,因此您将它们放在每个测试中的“常见”功能文件和调用中。请参阅: https://github.com/intuit/karate#shared-scope

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

孤凫 2025-01-31 05:51:20

对于自定义标题 - 在每个功能文件

  * configure headers = read('file:src/test/java/common/set-headers.js')

set -headers.js中 -

function fn() {
  return {
    "header1": header1_value,
    "header2": header2_value
  };
}

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
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文