我使用Json Path Extractor来获取Json数组。
这是我回复的一部分:
{"message":"SUCCESS",
"response_list":
[
{
"event":
{
"id":"123456",
"title":"Sie7e",
"venue_name":"New York, New York, United States",
"city_name":"New York",
"country_name":"United States",
"link":"http://newyorkcity.eventful.com/venues/new-york-new-york-united-states-/123456?utm_source=apis&utm_medium=apim&utm_campaign=apic",
"geo_lat":40.7127837,
"geo_lng":-74.0059413,
"time":1430715600000},
我有10个事件,每个事件都有我的场地名称,我需要检查所有事件是否包含"纽约" (这是按场地结果搜索)
在Json Path Extractor中,我的参数是:
目的地变量名称:venue_name
JSONPath表达式:$ .. venue_name
默认值:NOT_FOUND
我的BeanShell PostProcessor代码(完全在黑暗中拍摄):
venue_name = vars.get("venue_name");
for (int i=0 ; i<10 ; i++) {
if (!venue_name[i].contains("New York")) {
Failure = true;
FailureMessage = "YOU FAILED !!!!! venue name is = " + venue_name[i];
}
}
但我在日志中收到错误:
2015/05/17 12:42:22错误 - jmeter.util.BeanShellInterpreter:错误 调用bsh方法:eval源文件:内联评估:
venue_name = vars.get("venue_name"); for (int i=0 ; i<10 ; i++) { . . . '' : Not an array 2015/05/17 12:42:22 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:
venue_name = vars.get(&#34; venue_name&#34); for(int i = 0; i&lt; 10; i ++){。 。 。 &#39;&#39; :不是数组
我应该如何更正我的BeanShell代码?
答案 0 :(得分:1)
我建议稍微更新您的Beanshell代码,因为JSON Path Extractor不会公开匹配计数。如果添加Debug Sampler,您会看到“venue_name_”变量如下所示:
venue_name_1=New York, New York, United States
venue_name_2=New York, New York, United States
etc.
因此,您需要遍历所有变量,找到以“venue_name”开头的变量并检查它们的值。下面引用Beanshell代码(将其添加到Beanshell Assertion并确保它 JSON Path Extractor
Iterator iterator = vars.getIterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
if (entry.getKey().toString().startsWith("venue_name")) {
String value = entry.getValue().toString();
if (!value.contains("New York")) {
Failure = true;
FailureMessage = "venue_name value was: " + value;
}
}
}
有关在JMeter中使用Beanshell的更多信息,请参阅How to use BeanShell: JMeter's favorite built-in component指南。