如何在空手道框架中编译Javascript代码 [英] How to compile the Javascript code in Karate framework

查看:51
本文介绍了如何在空手道框架中编译Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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映射.

In above JSON, code attribute is mapped with certain javascript.

我必须验证代码属性的值.

I've to validate the value of code attribute.

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

The validation of value of code attribute should return true, if value of code attribute has no any compilation error.

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

The validation of value of code attribute should return false, if value of code attribute has compilation error using.

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

How should make possible to compile the javascript ( i.e. value of code attribute) as part of validation in Intuite/karate testing ?

推荐答案

您无法编译JS,但可以对其进行 eval 评估.如果您在理解以下内容时遇到麻烦,可以请您附近的一位懂JavaScript的人(他们可以实际阅读文档)帮忙.

You can't compile JS but you can eval it. Take the help of someone near you who knows JavaScript (and who can actually read the documentation) if you have trouble understanding the below.

* 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"
}
"""

这篇关于如何在空手道框架中编译Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆