Pre-requisite:
Inside JMeter bin folder, I have edited BeanShellFunction.bshrc
file to add my function as follows
String getMyString()
{
return "MyString";
}
I have enabled the BeanShellFunction.bshrc
from jmeter.properties
file as
beanshell.function.init = BeanShellFunction.bshrc
When I use the following syntax to call function it works fine.
${__BeanShell(getMyString())}
Question:
How can I call the same function from BeanShell Programs like PreProcessor, PostProcessor, Assertion, etc.?
Analysis:
I tried with following but no luck:
String myStr = getMyString();
It gives an error as:
Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` String myStr = getMyString(); print("MyStr: "+myStr);'' : Typed variable declaration : Command not found: getMyString()
From this SO post I found the solution: Calling Jmeter Functions from BeanShell Assertion Script
Solution
For each BeanShell Program type there are different beanshell.*.init
properties defined in bin/user.properties
:
beanshell.function.init=BeanShellFunction.bshrc
beanshell.preprocessor.init=BeanShellSampler.bshrc beanshell.postprocessor.init=BeanShellSampler.bshrc beanshell.assertion.init=BeanShellFunction.bshrc
Hence the same function which needs to be called from any program(preprocessor, postprocessor, etc) we need to copy the function to every .bshrc
file OR use same .bshrc
file for every program init property.
Syntax to use:
You need use the same syntax used for sending URL parameter:
String myStr = "${__BeanShell(getMyString())}";
This automatically calls the beanshell method from defined .bshrc
file.
For Advance Scripting
If your BeanShell function accepts a parameter:
String getMyString(String strParam)
{
return "MyString: "+strParam;
}
And you want to pass a property as a parameter to the BeanShell function, you can use following syntax:
String myStr = "${__BeanShell(getMyString("${__P(param1)}"))}";
Believe me it works and it does not give any syntax error.