Search code examples
javajmeterbeanshell

Accessing array in jmeter from one controller to another controller


I have written an array in beanshell assertion in jmeter as shown below.

String[] myList = {"CM_Name","OwnerID"};
for (int i = 0; i < myList.length; i++)
{              
vars.put("create_"+myList[i],ctx.getCurrentSampler().getArguments().argumentsAsMap.get(myList[i]));
log.info("create_"+myList[i]);
}

I want myList[] to be accessible in another beanshell assertion which is located in another controller. I have tried this

vars.put("myArr",myList);

But it didn't worked. What can i do to retrieve the above String array in another beanshell assertion ?


Solution

  • vars.put() method expects String only as the second argument therefore you cannot put an array there, the solutions are in:

    1. Use vars.putObject() method like:

      vars.putObject("myArr", myList);
      

      Later on you will be able to access it like:

      String [] myList = vars.getObject("myArr");
      
    2. Use bsh.shared namespace like:

      In first assertion:

       bsh.shared.myArr = myList
      

      In second assertion:

       String [] myList = bsh.shared.myArr
      

      This way you will even be able to share objects between different Thread Groups.

    See How to Use BeanShell: JMeter's Favorite Built-in Component article for more Beanshell-related tips and tricks