gpt4 book ai didi

Karate - 设置全局请求 header

转载 作者:行者123 更新时间:2023-12-05 06:28:11 24 4
gpt4 key购买 nike

所以我设法编写了一堆测试,并在每个功能文件中设置了相同的请求 header 。

例如:

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

我想知道是否有一种方法可以设置一次 header ,以便在每个场景运行之前设置它。我已经阅读了文档并尝试了 karate-config.js 中的 callSingle 方法:

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' }

以及我希望预设 header 的示例功能:

 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 方法的工作原理。一些指示会有所帮助。谢谢。

最佳答案

暂时忽略callSingle 并关注configure headers .

我认为您缺少一个步骤 - 即确保在每个 Scenario 之前“应用”configure headers。如果您 100% 确定这适用于“全局”,只需在 karate-config.js 中执行此操作:

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

否则您使用背景(在每个功能中):

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

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

关于 Karate - 设置全局请求 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54563246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com