空手道:在javascript中将字符串转换为空手道本机变量 [英] Karate: Convert string to karate native variable in javascript

查看:31
本文介绍了空手道:在javascript中将字符串转换为空手道本机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的测试自动化需要与 kafka 进行交互,我们正在研究如何使用空手道来实现这一点.

Our test automation needs to interact with kafka and we are looking at how we can achieve this with karate.

我们有一个 java 类,它从 kafka 读取并将记录放入内部列表中.然后我们从空手道请求这些记录,从后台流量中过滤掉所有消息,并返回与我们的过滤器匹配的第一条消息.

We have a java class which reads from kafka and puts records in an internal list. We then ask for these records from karate, filter out all messages from background traffic, and return the first message that matches our filter.

所以我们的消费者看起来像这样(简化):

So our consumer looks like this (simplified):

// consume.js
function(bootstrapServers, topic, filter, timeout, interval) {
  var KafkaLib = Java.type('kafka.KafkaLib')
  var records = KafkaLib.getRecords(bootstrapServers, topic)

  for (record_id in records) {
    // TODO here we want to convert record to a json (and later xml for xml records) so that
    // we can access them as 'native' karate data types and use notation like: cat.cat.scores.score[1]
    var record = records[record_id]
    if (filter(record)) {
      karate.log("Record matched: " + record)
      return record
    }
  }

  throw "No records found matching the filter: " + filter
}

记录可以是 json、xml 或纯文本,但现在查看 json 大小写.在这种情况下,考虑到在 kafka 中有这样的消息:{"correlationId":"b3e6bbc7-e5a6-4b2a-a8f9-a0ddf435de67","text":"Hello world"}这在上面的记录变量中作为字符串加载.

Records can be json, xml, or plain text, but looking in the json case now. In this case given that in kafka there is a message like this: {"correlationId":"b3e6bbc7-e5a6-4b2a-a8f9-a0ddf435de67","text":"Hello world"} This is loaded as a string in the record variable above.

我们想将其转换为 json 以便像这样的过滤器可以工作:

We want to convert this to json so that a filter like this would work:

* def uuid = java.util.UUID.randomUUID() + ''
# This is what we are publishing to kafka
* def payload = ({ correlationId: uuid, text: "Hello world" })
* def filter = function(m) { return m.correlationId == uuid }

有没有办法在 javascript 中将字符串转换为原生空手道变量?看 https://intuit.github.io/karate/# 可能错过了空手道对象.顺便说一下,var jsonRecord = karate.toJson(record) 不起作用,jsonRecord.uuid 未定义.

Is there a way to convert a string to a native karate variable in javascript? Might have missed it looking at https://intuit.github.io/karate/#the-karate-object. By the way var jsonRecord = karate.toJson(record) did not work and jsonRecord.uuid was undefined.

我在这里做了一个我想要实现的例子:https://github.com/KostasKgr/karate-issues/blob/java_json_interop/src/test/java/examples/consumption/consumption.feature

I have made an example of what I am trying to achieve here: https://github.com/KostasKgr/karate-issues/blob/java_json_interop/src/test/java/examples/consumption/consumption.feature

非常感谢

推荐答案

正如在另一个答案的评论中提到的,现在有一个关于空手道的增强票来实现这个线程中讨论的内容,请参阅 https://github.com/intuit/karate/issues/1202

As mentioned in the comments of another answer, there is now an enhancement ticket on karate to achieve what was discussed in this thread, see https://github.com/intuit/karate/issues/1202

在此之前,通过在 Java 中将字符串解析为 json 并将其返回给空手道,我设法获得了大部分我想要的关于 JSON 的内容.

Until that is in place, I managed to get most of what I wanted concerning JSON by parsing string to json in Java and returning that to karate.

Map<String,Object> result = new ObjectMapper().readValue(record, HashMap.class);

不确定是否可以为 xml 解决相同的问题

Not sure if the same can be worked around for xml

您可以在此处查看实际的解决方法:https://github.com/KostasKgr/karate-issues/blob/java_json_interop_v2/src/test/java/examples/consumption/consumption.feature

You can see the workaround in action here: https://github.com/KostasKgr/karate-issues/blob/java_json_interop_v2/src/test/java/examples/consumption/consumption.feature

这篇关于空手道:在javascript中将字符串转换为空手道本机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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