如何在空手道中使用参数在其他文件中调用JS函数?

发布于 2025-02-08 05:38:20 字数 835 浏览 2 评论 0原文

我有一个空手道功能,需要通过JS文件的参数调用JavaScript函数。不在功能本身中。想要在单独的文件中组织许多JS函数,并从功能文件中调用它。就像用黄瓜创建Java步骤定义一样。

Feature: Inside new
Background:
    * def result = call read(‘GetTokens.feature')
    Given url devURL
    * def at = result.access_token
    * def rt = result.refresh_token
    * def data = read('classpath:src/main/resources/GetHeaders.js', at, rt)
    * configure headers = data

Scenario Outline: new
    Given path ‘new’
    And request ‘’
    When method post
    And status 201

此功能称为getheaders.js,但无法阅读参数。被称为的方式有什么问题。或空手道框架/Nashorn无法支持。

#GetHeaders.js    
var f1 = function(at, rt){
    var head = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'token': at,
      'refresh-token': rt,
      'channel': 'online'
    }
    return head
}

I have a karate feature which needs to call a JavaScript function with arguments from js file. Not in the feature itself. Want to organise many js function in separate file and call it from a feature file. Just like creating java step definitions with cucumber.

Feature: Inside new
Background:
    * def result = call read(‘GetTokens.feature')
    Given url devURL
    * def at = result.access_token
    * def rt = result.refresh_token
    * def data = read('classpath:src/main/resources/GetHeaders.js', at, rt)
    * configure headers = data

Scenario Outline: new
    Given path ‘new’
    And request ‘’
    When method post
    And status 201

This feature calls GetHeaders.js But not able to read the arguments. Is there anything wrong with the way it's been called. Or karate framework/nashorn can't support.

#GetHeaders.js    
var f1 = function(at, rt){
    var head = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'token': at,
      'refresh-token': rt,
      'channel': 'online'
    }
    return head
}

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

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

发布评论

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

评论(3

山色无中 2025-02-15 05:38:20

将JS代码移至功能文件中的功能。就像其他任何JS或Java函数一样,可以使用任意数量的参数来调用该函数。以下是您需要任何参考的示例代码。

* def GetHeaders = 
"""
   function(at, rt){
       var head = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'token': at,
          'refresh-token': rt,
          'channel': 'online'
        }
   return head
   }
"""
* karate.log(GetHeaders('shfusdfhdskfds', 'ueworuewoew767638'))

Move the js code to a function within a feature file. The function can be called using any number of arguments just like any other js or java functions. Below is the sample code incase you want any reference.

* def GetHeaders = 
"""
   function(at, rt){
       var head = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'token': at,
          'refresh-token': rt,
          'channel': 'online'
        }
   return head
   }
"""
* karate.log(GetHeaders('shfusdfhdskfds', 'ueworuewoew767638'))
北城挽邺 2025-02-15 05:38:20

尝试以这种格式调用JS代码。它应该起作用

* def data = call read('classpath:src/main/resources/GetHeaders.js@f1') {at:#(at), rt:#(rt)}

Try calling the js code in this format. It should work

* def data = call read('classpath:src/main/resources/GetHeaders.js@f1') {at:#(at), rt:#(rt)}
默嘫て 2025-02-15 05:38:20

空手道不支持从外部文件调用JS方法

karate does not support calling js methods from external files

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