Api 如何使用空手道工具比较包含数组的2个JSON对象

Api 如何使用空手道工具比较包含数组的2个JSON对象,api,testing,karate,Api,Testing,Karate,使用intuit/空手道的API测试之一 所需的JSON为:{name:hello, 配置:[{用户名:abc,密码:xyz},{用户名:qwe,密码:tyu}]} API响应有两种可能性 第一个可能的实际JSON:{name:hello, 配置:[{用户名:qwe,密码:tyu},{用户名:abc,密码:xyz}]} 第二种可能的实际JSON:{name:hello, 配置:[{用户名:abc,密码:xyz},{用户名:qwe,密码:tyu}]} 同样,数组元素的顺序在实际响应中是不同的,因此

使用intuit/空手道的API测试之一

所需的JSON为:
{name:hello,
配置:[{用户名:abc,密码:xyz},{用户名:qwe,密码:tyu}]}

API响应有两种可能性

第一个可能的实际JSON:
{name:hello,
配置:[{用户名:qwe,密码:tyu},{用户名:abc,密码:xyz}]}

第二种可能的实际JSON:
{name:hello,
配置:[{用户名:abc,密码:xyz},{用户名:qwe,密码:tyu}]}

同样,数组元素的顺序在实际响应中是不同的,因此下面的响应验证方法会随机抛出错误

  • 和响应==
  • 并且响应包含
有时会抛出以下错误: 错误:{Actual:response.config[0].abc,应为:response.config[0].qwe}

有时会抛出以下错误: 错误:{Actual:response.config[0].qwe,应为:response.config[0].abc}


请您提供确切的JSON验证空手道方法,在这种方法中,整个JSON以及包含JSON的数组中忽略元素的序列

您需要仔细阅读文档:


如果响应API返回的值与response1或response2的值完全相同,则上述答案有效。@NageshPathrut no。最后一行将适用于这两种情况。你读过文档了吗?配置:“#”(^^config)”也会验证数组中单个元素的数据吗?我必须测试数组元素exist of not以及if exist,然后它的数据是否与数组中的一个预期元素匹配。@NageshPathrut yes。试试看。您可以将该示例粘贴到
场景中
并查看它的工作情况,即使不进行任何HTTP调用也是如此。实际上,此验证是在一个函数中进行的,我正在将预期的完整JSON传递给该函数。既然我必须从传递的json中获取其他根级别属性(如name)的值,那么如何使用这种技术实现这一点呢。
* def response1 = {name: 'hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
* def response2 = {name: 'hello', config:[{username: 'abc', password: 'xyz'},{username: 'qwe', password: 'tyu'}]}

* def config = [{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]
* match response1 == { name: 'hello', config: '#(config)' }
* match response2 == { name: 'hello', config: '#(^^config)' }