如何验证 bean shell 中的 json 响应并在响应与 jmeter 中的预期不符时执行某些操作?
How to validate json response in bean shell and perform some action in case the response is not as expected in jmeter?
我想在 jmeter 中提取 json 响应,如果响应不符合预期,我需要将其打印到 csv 文件。
我尝试使用 contains 方法检查响应是否包含预期的关键字,但它似乎不起作用。我还有其他方法可以做到这一点吗?
示例代码在这里:
log.info(ctx.getPreviousResult().getResponseDataAsString());
r = ctx.getPreviousResult().getResponseCode();
d = ctx.getPreviousResult().getResponseDataAsString();
if(!d.contains("valid")){
p.println(r +","+ vars.get("email") +",");
}
这是我的 json 回复
{
"isBlueLinkServicePinValid": "valid"
}
我正在检查关键字 "valid"
if(!d.contains("valid"))
但是好像不行?
TIA
Since JMeter 3.1 it is not recommended to use Beanshell for scripting, you should go for JSR223 Test Elements and Groovy language instead. The main reason is that Groovy has much better performance than Beanshell 确实如此。
Groovy has built-in JSON support 因此您可以轻松提取 isBlueLinkServicePinValid
属性值:
String response = prev.getResponseDataAsString();
log.info("Response: " + response)
String valid = new groovy.json.JsonSlurper().parseText(response).isBlueLinkServicePinValid
log.info("Valid: " + valid);
if (valid.equals("valid")) {
log.info("Do something");
}
else {
log.info("Do something else");
}
演示:
我想在 jmeter 中提取 json 响应,如果响应不符合预期,我需要将其打印到 csv 文件。
我尝试使用 contains 方法检查响应是否包含预期的关键字,但它似乎不起作用。我还有其他方法可以做到这一点吗?
示例代码在这里:
log.info(ctx.getPreviousResult().getResponseDataAsString());
r = ctx.getPreviousResult().getResponseCode();
d = ctx.getPreviousResult().getResponseDataAsString();
if(!d.contains("valid")){
p.println(r +","+ vars.get("email") +",");
}
这是我的 json 回复
{
"isBlueLinkServicePinValid": "valid"
}
我正在检查关键字 "valid"
if(!d.contains("valid"))
但是好像不行?
TIA
Since JMeter 3.1 it is not recommended to use Beanshell for scripting, you should go for JSR223 Test Elements and Groovy language instead. The main reason is that Groovy has much better performance than Beanshell 确实如此。
Groovy has built-in JSON support 因此您可以轻松提取
isBlueLinkServicePinValid
属性值:String response = prev.getResponseDataAsString(); log.info("Response: " + response) String valid = new groovy.json.JsonSlurper().parseText(response).isBlueLinkServicePinValid log.info("Valid: " + valid); if (valid.equals("valid")) { log.info("Do something"); } else { log.info("Do something else"); }
演示: