Karate全局函数未定义异常。

huangapple go评论57阅读模式
英文:

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: &quot;appendGlobalHeaders&quot; is not defined

In karate-config.js:

karate.configure(&#39;headers&#39;, {
  &#39;Content-Type&#39;: &#39;application/json&#39;,
  &#39;Authorization&#39;: &#39;Bearer &lt;your-token&gt;&#39;
});

function appendGlobalHeaders(headers) {
  var globalHeaders = karate.get(&#39;headers&#39;);
  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 &#39;https://api.example.com/users&#39;
    * def requestHeaders = appendGlobalHeaders({ &#39;X-Custom-Header&#39;: &#39;Custom-Value&#39; })
    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使用headerheaders关键字设置标头,而根本不使用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 = { 
      &#39;Content-Type&#39;: &#39;application/json&#39;,
      &#39;Authorization&#39;: &#39;Bearer &lt;my-token&gt;&#39;,
      &#39;Extra-Header&#39;: karate.get(&#39;myVariable&#39;)
    };
    return headers;
  };
  karate.configure(&#39;headers&#39;, headersFn);
}

And in your feature you can do this:

* def myVariable = &#39;foo&#39;
* url &#39;https://httpbin.org/get&#39;
* 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(&#39;X-Custom-Header&#39;, &#39;Custom-Value&#39;)

So all you need to do is define a function in scope called genHeaders that returns a JSON.

huangapple
  • 本文由 发表于 2023年7月12日 23:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定