如何使用空手道框架验证 api 响应中的时间戳是否是最新的

how to validate whether the timestamp from an api response is the latest using karate framework

我收到了 api 使用空手道框架的回复,是这样的..

{
"name": "xyz.json",
"fileSize": "391 B",
"timestamp": "2020-06-22 12:03:00 GMT",
"tag": "abc1"
},
{
"name": "abc.json",
"fileSize": "391 B",
"timestamp": "2020-06-22 12:03:01 GMT",
"tag": "abc2"
},

现在从上面的响应中我如何验证“时间戳”字段,即最新的 2 个时间戳值,以便我可以从名称字段中获取相应的 json 文件。

您可以这样将日期转换为数字:

* def response =
"""
[
   {
      "name":"xyz.json",
      "fileSize":"391 B",
      "timestamp":"2020-06-22 12:03:00 GMT",
      "tag":"abc1"
   },
   {
      "name":"abc.json",
      "fileSize":"391 B",
      "timestamp":"2020-06-22 12:03:01 GMT",
      "tag":"abc2"
   }
]
"""
* def dateToLong =
"""
function(s) {
  var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
  var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
  return sdf.parse(s).time;
} 
"""
* def fun = function(x){ return dateToLong(x.timestamp) }
* def dates = karate.map(response, fun)
* print dates

我留给你想办法得到最新的。空手道不是通用语言,但您可以混合使用 Java 脚本,或者我建议编写 Java 实用程序,请参阅文档了解具体方法。还要查看 JSON 转换:https://github.com/intuit/karate#json-transforms