Search code examples
jmeterbeanshell

Calling class in Jmeter Beanshell


I want to call method in beanshell sampler from other beanshell sampler. The best way is to call from external jar but is it possible to interaction with each other.


Solution

  • There is bsh.shared namespace which can be used for sharing variables and more complex objects, i.e.

    1. In first sampler you can do something like:

      MyClass someClass = new MyClass();
      bsh.shared.someClass = someClass();
      
    2. In second sampler you can refer the instance of the class like:

      MyClass someClass = bsh.shared.someClass();
      someClass.someUsefulFunction();
      

    Also be aware that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so you might want migrate to Groovy.

    JSR223 Test Elements don't have access to this bsh.shared namespace but you can use props shorthand for this purpose:

    1. In first sampler:

      MyClass someClass = new MyClass();
      props.put("someClass", someClass);
      
    2. In second sampler:

      MyClass someClass = props.get("someClass");
      //etc.