[英]Issue with groovy response assertion script in jmeter
I want to write a code for Response assertion using groovy script , for the Response data like this: 我想使用groovy脚本为Response断言编写代码,对于Response数据,如下所示:
[
{
"fieldId":"947bb60f",
"id":"e7b8ad2b",
"name":"field",
}
]
Tried using the below groovy script for which i am getting error(failure message). 尝试使用下面的groovy脚本,我收到错误(失败消息)。
if (!jsonResponse.keySet().containsAll(["fieldId","id","name"] )) {
failureMessage += "The json response body has wrong structure or error msg.\n\n";
}
The same script working fine with the single tree structure as below. 相同的脚本与单树结构一起正常工作如下。 Appreciate your help on this with groovy script .
通过groovy脚本欣赏你的帮助。
[
"fieldId":"947bb60f",
"id":"e7b8ad2b",
"name":"field",
]
So, you are getting a list of items returned (containing a single item) 因此,您将获得返回的项目列表(包含单个项目)
Assuming you never expect more than one item, you can check the size of it with: 假设您从不期望有多个项目,您可以通过以下方式检查它的大小:
if (jsonResponse.size() != 1) {
failureMessage += "Expected one item, got ${jsonResponse.size()}.\n\n";
}
Then, you can grab the first element with: 然后,你可以抓住第一个元素:
def jsonElement = jsonResponse[0]
And check the field names with: 并检查字段名称:
if (jsonElement.keySet() != ["fieldId","id","name"] as Set) {
failureMessage += "Unexpected fields in response ${jsonElement.keySet()}.\n\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.