Store a Jmeter JSR223 script in a variable

Viewed 317

I have a Jmeter script with many HTTP Requests, each with a JSR223 preprocessor. Most of the preprocessors have common code and I want to reference that code from one place, not necessarily functions but a large piece of code that is duplicated in all preprocessors. I've experimented with a few things and I came across in the Jmeter documentation the mention of storing a script in a JSR223 Sampler and then in a variable and then referencing the variable with ${__groovy(${RANDOM_NAME})}.

https://jmeter.apache.org/usermanual/best-practices.html#developing_scripts

https://jmeter.apache.org/usermanual/functions.html#how

I cannot find any examples of this working in a real script. I understand most of this but I am not sure how "it can be stored as a variable on the Test Plan". I have the code in a JSR223 sample in a thread group that is called before all others but am not sure how to store it as a variable. Can someone please show me a working example of this?

2 Answers

I think a more suitable solution is to use same Script File in JSR223 sampler

Script File

Name of a file to be used as a JSR223 script, if a relative file path is used, then it will be relative to directory referenced by "user.dir" System property

But you can save script as JMeter property, for example in user.properties

  MyScript=[script]

And define in Test plan a variable with value ${__P(MyScript)}

Related