英文:
Karate: checking value in array whether any of the value is blank or null
问题
{
"result": [
{
"hostname": "CDGRPASEAAR001",
"critical": 0,
"minor": 1,
"syslog": 0
},
{
"hostname": "MUMBCHUDAAR001",
"critical": 0,
"minor": 0,
"syslog": 0
},
{
"hostname": "",
"critical": 0,
"minor": 1,
"syslog": 0
}
]
}
英文:
Here is the sample json
{
"result": [
{
"hostname": "CDGRPASEAAR001",
"critical": 0,
"minor": 1,
"syslog": 0
},
{
"hostname": "MUMBCHUDAAR001",
"critical": 0,
"minor": 0,
"syslog": 0
},
{
"hostname": "",
"critical": 0,
"minor": 1,
"syslog": 0
},
I am trying below:
And match actualResponse == {result[2].hostname: '##notnull'}
But I am getting this validation message:
> reason: actual value has 4 more key(s) than expected
答案1
得分: 1
我认为这是你想要的:
* 匹配 response.result[2] 包含 { hostname: ''##notnull'' }
这也可以:
```plaintext
* 匹配每个 response.result 包含 { hostname: ''#string'' }
所以请查阅文档:https://github.com/karatelabs/karate#fuzzy-matching
如果你想要找到一些计数,那么 `match` 不是你想要的。你需要开始进行转换,比如 `filter()`。参考:https://stackoverflow.com/a/74459339/143475 | https://stackoverflow.com/a/75425063/143475
例如:
```plaintext
* 定义 names = response.result.filter(x => x.hostname !== '''').map(x => x.hostname)
* 匹配 names == ['CDGRPASEAAR001', 'MUMBCHUDAAR001']
英文:
I think this is what you were trying for:
* match response.result[2] contains { hostname: '##notnull' }
This would also work:
* match each response.result contains { hostname: '#string' }
So please go through the docs: https://github.com/karatelabs/karate#fuzzy-matching
If you are trying to find some count, then match
is not what you want. You have to start doing transforms, like filter()
. Refer: https://stackoverflow.com/a/74459339/143475 | https://stackoverflow.com/a/75425063/143475
For example:
* def names = response.result.filter(x => x.hostname !== '').map(x => x.hostname)
* match names == ['CDGRPASEAAR001', 'MUMBCHUDAAR001']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论