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?
2 Answers
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:
- Compile your Groovy "common" code into a .jar library
- Put the library somewhere in JMeter Classpath
- Restart JMeter to pick up the .jar file with your common functions
- Enjoy
More information: How to Reuse Your JMeter Code with JAR Files and Save Time

- 159,985
- 5
- 83
- 133
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.

- 313
- 1
- 6