0

I am using selenium with JMeter and have written number of methods in groovy. But the challenge I am facing is, in each sampler, I have to define all the created methods. Is there any way we can call the methods created from a common place and use it anywhere in any sampler in Jmeter?

user71899
  • 17
  • 1
  • 6

2 Answers2

0

JMeter-only solution would be placing your "common" functions into separate JSR223 Samplers living under Test Fragments and invoking them where required using Module Controllers

For "classic" way of re-using the code:

  1. Compile your Groovy "common" code into a .jar library
  2. Put the library somewhere in JMeter Classpath
  3. Restart JMeter to pick up the .jar file with your common functions
  4. Enjoy

More information: How to Reuse Your JMeter Code with JAR Files and Save Time

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

To create methods in a central place that you can re-use:

In a JSR223 sampler set to use groovy, create the reusable function, such as this one:

String logCommon() {
    String threadNumber = ctx.getThreadNum().toString();
    return "Thread #: " + threadNumber;
}
props.put('logging_common', this.&logCommon); 

Then create a JSR223 Sampler again with groovy somewhere later and call the function:

String logCommon = props.get('logging_common')();
log.info(logCommon);

This pattern helps centralise your code.

Note, the samplers can be in different threadgroups because the function is stored as a property (using 'props'). If the function was stored a variable ('vars' instead), then both samplers would need to be in the same threadgroup.

Aidan
  • 313
  • 1
  • 6