Jmeter 从带有验证的json响应中选择随机值
当我向api端点发送请求时,下面的json将出现在我的响应中Jmeter 从带有验证的json响应中选择随机值,jmeter,Jmeter,当我向api端点发送请求时,下面的json将出现在我的响应中 { "entities": { "practitioners": { "f1d26a4b-c489-493d-bccf-7b9c8b92ecac": { "fullAvatarUrl": null, "id": "f
{
"entities": {
"practitioners": {
"f1d26a4b-c489-493d-bccf-7b9c8b92ecac": {
"fullAvatarUrl": null,
"id": "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "1",
"type": "Dentist",
"isActive": true,
"isHidden": false
},
"ee87642d-c9a6-4a9d-b99a-a96501f27a7b": {
"fullAvatarUrl": null,
"id": "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "2",
"type": "Hygienist",
"isActive": true,
"isHidden": false
},
"d0aeb9eb-f267-45ad-8cdf-eada1155c274": {
"fullAvatarUrl": null,
"id": "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "3",
"type": "Dentist",
"isActive": true,
"isHidden": false
},
"2f641e8e-c5d6-4fdf-8fbe-f99fe837f441": {
"fullAvatarUrl": null,
"id": "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "4",
"type": "Hygienist",
"isActive": true,
"isHidden": false
}
}
},
"result": [
"f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
"ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
"d0aeb9eb-f267-45ad-8cdf-eada1155c274",
"2f641e8e-c5d6-4fdf-8fbe-f99fe837f441"
]
}
问题陈述:我想从上述4个随机值中选择任何一个(想选择从业者id,并且位于从业者下的第一个元素),并验证isActive
应该是true
,isHidden
应该是false
我尝试使用JSON提取器,使用表达式$.entities.practices
和匹配号0
但它不是选择任何一个,而是选择所有。我想这种方法可以帮助你 在http请求中添加一个
JSR223后处理器
,然后请将以下groovy脚本添加到后处理器
注意:这只是为了选择随机实践ersid
进行验证,只需使用类似的逻辑即可
import groovy.json.*
def response = prev.responseDataAsString ;
def json = new JsonSlurper().parseText(response) ;
def sizeResultPractitioners = json.result.size();
Random rnd = new Random()
def randomResultPractitioners = rnd.nextInt(sizeResultPractitioners);
log.info("---------->"+randomResultPractitioners);
log.info("---------->"+json.result[randomResultPractitioners]);
${randomId}
的形式访问随机ID
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def ids = []
json.entities.practitioners.each { practitioner ->
if (practitioner.value.isActive && !practitioner.value.isHidden) {
ids.add(practitioner.key)
}
}
def randomId = ids.get(org.apache.commons.lang3.RandomUtils.nextInt(0, ids.size()))
vars.put('randomId', randomId)