groovyjmeter

How can I return and read a String[] variable from a ${__groovy("cmd")}?


I have a csv file where I need to randomly pick a line and then separately access each column for that line. I cannot use the available CSV config element(s) as I need to control the random seed value.

So trying to return a String[] (or a List) from a __groovy("cmd") call and then use each field separately. I tried the below but the __groovy call to return a String[] generates "dimensions cannot be all empty" and for a List I get "You cannot create an instance from the abstract interface 'java.util.List'".

How can I do this? Sample code (for String[] ) below:

def events = [[1,"A"],[2,"B"]];
vars.putObject("events",events);

String getEvent = "new String[2](vars.getObject('events')[2].split(','))";

The call I like to use from a sampler is something like:

${__groovy(${getEvent})[2]} // example should return B

Solution

  • If you want to create a List like you do and want to be able to look for values basing on Integer numbers you can do this using __groovy() function like:

    ${__groovy(vars.getObject('events').find { it[0] == 2 }?.get(1),)}
    

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?