Sid*_*ish 3 jmeter beanshell jsr223 jmeter-3.2
我有一个示例 JSON 响应,如下所示:
{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
"code": "color",
"label": "Color",
"extra_info": "",
"values": [{
"label": "Red",
"value": "8"
}, {
"label": "Yellow",
"value": "9"
}, {
"label": "Pink",
"value": "10"
}]
}, {
"code": "size",
"label": "Size",
"extra_info": "",
"values": [{
"label": "Small",
"value": "4"
}, {
"label": "Medium",
"value": "5"
}, {
"label": "Large",
"value": "6"
}]
}],
"options_available": [{
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Large"
}]
}],
"custom_options": []
}
Run Code Online (Sandbox Code Playgroud)
我的采样器为
import org.json.JSONObject;
import org.json.JSONArray;
String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);
Run Code Online (Sandbox Code Playgroud)
但是在运行脚本时我收到错误:
No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)
除了值之外(计数=2)。我的目的是获取“Options”键中的数组计数(参见上面的响应)
vars.put不支持与常规值不同的值String,并且您正在尝试输入Integer值。
通过简单的转换即可轻松解决:
vars.put("counts", Integer.toString(count));
Run Code Online (Sandbox Code Playgroud)
另一个选项是使用vars.putObject保存对象
vars.putObject("counts", count);
Run Code Online (Sandbox Code Playgroud)
根据 JMeter最佳实践,还可以转向 JSR223 Sampler :
从 JMeter 3.1 开始,我们建议从 BeanShell 切换到 JSR223 测试元素
| 归档时间: |
|
| 查看次数: |
6252 次 |
| 最近记录: |