0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If I get it right, this code fails me at the first "not contains" value and shows the responded value in the failure message.
Yes, the code checks all variables starting with "venue_name" values and if any of them doesn't contain search criteria (New York) - assertion and the sampler will be marked as failed.
What if I need to see all the failed values and also know where is failed and not only the value string since then I'll have to find manually the position of the failed value in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.