【问题标题】:Getting `org.graalvm.polyglot.PolyglotException: ReferenceError:` - after upgrading to karate 1.1.0.RC3 from karate 0.9.5 [duplicate]获取`org.graalvm.polyglot.PolyglotException:ReferenceError:`-从空手道0.9.5升级到空手道1.1.0.RC3后[重复]
【发布时间】:2021-06-30 06:15:13
【问题描述】:

说明:升级到空手道 1.1.0.RC3 后出现参考错误消息

步骤: 执行 test.feature 文件

test.feature

  Scenario: Get custom request
    * def testUtils = call read('classpath:features/utils.feature')
    * def getRequest = testUtils.customRequest()
    * print getRequest 

Utils.feature

Feature: common utilities

  Scenario: 
    * def randomAlphabetic = function (count) { return org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(count) }

    * def customRequest =
    """
     function() {
     var name = randomAlphabetic(2)
     return {
         name:name,
         city:'Bangalore'
     }
  }
    """

错误信息:

org.graalvm.polyglot.PolyglotException: ReferenceError: "randomAlphabetic" is not defined
- <js>.:anonymous(Unnamed:2)

Git repo 供参考 - https://github.com/naveenkrao/karate-sample-project

【问题讨论】:

    标签: karate


    【解决方案1】:

    在新版本中重复使用 JS 函数有一些限制。有多种解决方法。下面是一个,用于在 JS 块本身内移动可重用函数:

    * def customRequest =
    """
     function() {
     var randomAlphabetic = function (count) { return java.lang.System.currentTimeMillis() + '' };
     var name = randomAlphabetic(2);
     return {
         name:name,
         city:'Bangalore'
     }
    }
    """
    

    或者,如果您使 randomAlphabetic 函数全局可用,例如通过karate-config.js 它也可以工作。

    这是我们努力解决的 JS 引擎更改的结果。如果这对您来说是个问题,欢迎您贡献代码。我们认为不是,有很多方法可以创建可重用的函数,遵循一些规则——保持简单,更喜欢 Java 来处理复杂的逻辑,不要过多地混合或组合 JS 或 Java。

    如果您认为需要更新文档,也欢迎您提供文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      相关资源
      最近更新 更多