如何在空手道框架中编译Javascript代码

如何在空手道框架中编译Javascript代码,javascript,cucumber,karate,Javascript,Cucumber,Karate,我对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中,

我对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中,代码属性与特定javascript映射

我必须验证代码属性的值

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

如果代码属性的值有编译错误,则代码属性值的验证应返回false


作为Intuite/空手道测试验证的一部分,应如何编译javascript(即代码属性的值)

你不能编译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"
}
"""

您不能编译JS,但可以
eval
JS。如果您在理解以下内容时遇到困难,请向熟悉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"
}
"""