[英]How to compare 2 JSON objects containing array using Karate tool
One of the API testing using intuit/karate,使用直觉/空手道的 API 测试之一,
Expected JSON is: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}
预期的 JSON 是:
{name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}
There is two possibility of an API response. API 响应有两种可能性。
First possible actual JSON: {name: hello, config:[{username: qwe, password: tyu},{username: abc, password: xyz}]}
第一个可能的实际 JSON:
{name: hello, config:[{username: qwe, password: tyu},{username: abc, password: xyz}]}
Second possible actual JSON: {name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}
第二种可能的实际 JSON:
{name: hello, config:[{username: abc, password: xyz},{username: qwe, password: tyu}]}
Likewise, the sequence of array element is different in actual response, hence following approach of validation of response throws error randomly.同样,数组元素的顺序在实际响应中是不同的,因此遵循响应验证的方法会随机抛出错误。
Sometimes error is thrown as : Error : { Actual: response.config[0].abc, Expected: response.config[0].qwe }有时错误被抛出为:错误:{实际:response.config[0].abc,预期:response.config[0].qwe}
Sometimes error is thrown as : Error : { Actual: response.config[0].qwe, Expected: response.config[0].abc }有时会抛出错误:错误:{实际:response.config[0].qwe,预期:response.config[0].abc}
Would you please provide exact karate approach of JSON validation in which entire JSON along with ignore the sequence of element in JSON containing array ?您能否提供 JSON 验证的精确空手道方法,其中整个 JSON 以及忽略包含数组的 JSON 中的元素序列?
You need to read the docs carefully: https://github.com/intuit/karate#contains-short-cuts您需要仔细阅读文档: https : //github.com/intuit/karate#contains-short-cuts
* 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)' }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.