Reputation: 161
I'm using Json Path Extractor to get Json array.
This is part of my response:
{"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},
I have 10 events which under each one I have venue_name and I need to check if all of them contains "New York" (this is a search by venue result)
In Json Path Extractor my parameters are:
Destination Variable Name: venue_name
JSONPath Expression: $..venue_name
Default Value: NOT_FOUND
My BeanShell PostProcessor code (which is completely shot in the dark):
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];
}
}
But I'm getting an error in my log:
2015/05/17 12:42:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of:
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("venue_name"); for (int i=0 ; i<10 ; i++) { . . . '' : Not an array
How should I correct my BeanShell code?
Upvotes: 0
Views: 3108
Reputation: 167992
I would suggest slightly update your Beanshell code as JSON Path Extractor doesn't expose matches count. If you add a Debug Sampler you'll see that "venue_name_" variables will look like:
venue_name_1=New York, New York, United States
venue_name_2=New York, New York, United States
etc.
So you need to iterate through all variables, find ones which start with "venue_name" and check their values. Reference Beanshell code below (add it to Beanshell Assertion and make sure that it is below 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;
}
}
}
For more information on using Beanshell in JMeter see How to use BeanShell: JMeter's favorite built-in component guide.
Upvotes: 1