具有依赖变量的 haders JS 文件
haders JS file with dependable of the variables
首先空手道是 API 自动化的最佳工具。时期。 :D
我已经设置了一个名为 headers.js 的文件。由于 headers 之一包含“语言环境变量”,例如 -> var headers = { 'X-Country-Code': "US" ...} 我需要它作为全局变量才能根据它的值设置其他变量,但由于它专门用于 headers 函数,我如何在 karate.feature 文件中访问它的值?
JS headers.js
function fn() {
var env = 'us';
if (!env) {
env = 'us';
}
var headers = { 'X-Country-Code': "us", 'Content-Type': 'application/x-www-form-urlencoded' };
if (!env) {
env = 'ca';
}
var headers = { 'X-Country-Code': "ca", 'Content-Type': 'application/x-www-form-urlencoded' };
return headers;
}
也许您需要做的就是通过 karate-config.js
设置一个全局变量,然后在其他地方使用它,包括 headers JS 函数。
无论如何,在极端情况下你可以从JS设置一个变量,这个在文档中有提到,但很容易错过:https://github.com/intuit/karate#configure-headers
In rare cases you may need to set a variable from this routine, and a good example is to make the generated UUID "visible" to the currently executing script or feature. You can easily do this via karate.set('someVarName', value)
.
所以这应该有效:
karate.set('currentCountryCode', headers['X-Country-Code']);
但这仅在功能文件中可用 在 发出 HTTP 请求之后,因为“headers 函数”是在 HTTP 调用之前调用的。如果你在 before 之前需要它,你必须想出一个不同的方法,并注意 karate.get('someVariableName')
可以做 karate.set()
的相反操作。
另请记住,如果您想在发出 HTTP 请求后获得 headers,您可以使用 karate.prevRequest.headers
。
首先空手道是 API 自动化的最佳工具。时期。 :D
我已经设置了一个名为 headers.js 的文件。由于 headers 之一包含“语言环境变量”,例如 -> var headers = { 'X-Country-Code': "US" ...} 我需要它作为全局变量才能根据它的值设置其他变量,但由于它专门用于 headers 函数,我如何在 karate.feature 文件中访问它的值?
JS headers.js
function fn() {
var env = 'us';
if (!env) {
env = 'us';
}
var headers = { 'X-Country-Code': "us", 'Content-Type': 'application/x-www-form-urlencoded' };
if (!env) {
env = 'ca';
}
var headers = { 'X-Country-Code': "ca", 'Content-Type': 'application/x-www-form-urlencoded' };
return headers;
}
也许您需要做的就是通过 karate-config.js
设置一个全局变量,然后在其他地方使用它,包括 headers JS 函数。
无论如何,在极端情况下你可以从JS设置一个变量,这个在文档中有提到,但很容易错过:https://github.com/intuit/karate#configure-headers
In rare cases you may need to set a variable from this routine, and a good example is to make the generated UUID "visible" to the currently executing script or feature. You can easily do this via
karate.set('someVarName', value)
.
所以这应该有效:
karate.set('currentCountryCode', headers['X-Country-Code']);
但这仅在功能文件中可用 在 发出 HTTP 请求之后,因为“headers 函数”是在 HTTP 调用之前调用的。如果你在 before 之前需要它,你必须想出一个不同的方法,并注意 karate.get('someVariableName')
可以做 karate.set()
的相反操作。
另请记住,如果您想在发出 HTTP 请求后获得 headers,您可以使用 karate.prevRequest.headers
。