【问题标题】:How to compile the Javascript code in Karate framework [duplicate]如何在空手道框架中编译 Javascript 代码 [重复]
【发布时间】:2019-02-06 12:08:55
【问题描述】:

我的 JSON 如下:

 {
  "id" : 6759,
  "type" : "JAVASCRIPT",
  "code" : "var result = request.getBody().tostring.indexOf('locate') || request.getBody().tostring.indexOf('find')",
  "description" : "This filters checks synonym of search word"
}

在上面的 JSON 中,code 属性是用特定的 javascript 映射的。

我要验证代码属性的值。

如果code属性的值没有任何编译错误,则code属性值的验证应该返回true。

如果code属性值使用编译错误,code属性值的验证应该返回false。

应该如何编译 javascript(即 code 属性的值)作为 Intuite/karate 测试中验证的一部分?

【问题讨论】:

    标签: javascript cucumber karate


    【解决方案1】:

    你不能编译 JS 但你可以eval 它。如果您无法理解以下内容,请向您附近了解 JavaScript(并且可以实际阅读文档)的人寻求帮助。

    * def script = "request.getBody().tostring.indexOf('locate') || request.getBody().tostring.indexOf('find')"
    * def code = true
    * eval try { karate.eval(script) } catch (e) { karate.set('code', false) }
    * print code
    * def response =
    """
    {
      "id" : 6759,
      "type" : "JAVASCRIPT",
      "code" : "#(code)",
      "description" : "This filters checks synonym of search word"
    }
    """
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多