英文:
Karate global function is undefined exception
问题
无法从场景中调用appendGlobalHeaders
,出现org.graalvm.polyglot.PolyglotException: ReferenceError: "appendGlobalHeaders"未定义
。
在karate-config.js
中:
karate.configure('headers', {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-token>'
});
function appendGlobalHeaders(headers) {
var globalHeaders = karate.get('headers');
return karate.append(headers, globalHeaders);
}
在Karate测试文件中:
Feature: 向请求头部添加全局头部
Scenario: 使用附加的头部进行API请求
Given url 'https://api.example.com/users'
* def requestHeaders = appendGlobalHeaders({ 'X-Custom-Header': 'Custom-Value' })
When method GET
Then status 200
英文:
Unable to call appendGlobalHeaders
from scenario, getting org.graalvm.polyglot.PolyglotException: ReferenceError: "appendGlobalHeaders" is not defined
In karate-config.js
:
karate.configure('headers', {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-token>'
});
function appendGlobalHeaders(headers) {
var globalHeaders = karate.get('headers');
return karate.append(headers, globalHeaders);
}
In Karate test file:
Feature: Append Global Headers to Request Headers
Scenario: Make an API request with appended headers
Given url 'https://api.example.com/users'
* def requestHeaders = appendGlobalHeaders({ 'X-Custom-Header': 'Custom-Value' })
When method GET
Then status 200
答案1
得分: 1
你没有正确实现karate-config.js
或理解Karate变量。我也认为你应该尝试一种不同的方法。
为什么不在karate-config.js
中尝试这个(这是完整的代码):
function fn() {
var headersFn = function() {
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <my-token>',
'Extra-Header': karate.get('myVariable')
};
return headers;
};
karate.configure('headers', headersFn);
}
然后在你的特性文件中可以这样做:
* def myVariable = 'foo'
* url 'https://httpbin.org/get'
* method get
尝试一下,看看它是如何工作的。
一个好处是,如果一个值是null
,Karate不会设置该标头。你可以利用这一点来实现动态的效果。请注意,你可以按照文档中解释的条件添加标头。
也许与所有这些复杂性相比,你可以选择一个简单的方法,其中每个Scenario
使用header
或headers
关键字设置标头,而根本不使用configure
。这完全取决于你。
另一种你可以尝试的方法是:
* headers genHeaders('X-Custom-Header', 'Custom-Value')
所以你只需要在作用域中定义一个名为genHeaders
的函数,它返回一个JSON。
英文:
You have not implemented karate-config.js
correctly or understood Karate variables. I also think you should try a different approach.
Why don't you try this in karate-config.js
(this is the complete code):
function fn() {
var headersFn = function() {
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <my-token>',
'Extra-Header': karate.get('myVariable')
};
return headers;
};
karate.configure('headers', headersFn);
}
And in your feature you can do this:
* def myVariable = 'foo'
* url 'https://httpbin.org/get'
* method get
Try it and see how it works.
And one good thing is that if a value is null
, Karate won't set that header. You can use that to your dynamic advantage. Note that you can conditionally add headers with if-then conditions as explained in the documentation.
Maybe instead of all this complexity, you may choose to just go with a simple approach where each Scenario
sets headers using the header
or headers
keyword and you don't use configure
at all. That's perfectly fine and up to you.
Another approach you could try is something like this:
* headers genHeaders('X-Custom-Header', 'Custom-Value')
So all you need to do is define a function in scope called genHeaders
that returns a JSON.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论