1
votes

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())}

It works fine for below case:
BeanShell Function Call

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();

BeanShell Assertion

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()

BeanShell Error

2

2 Answers

1
votes
  1. Add the next line to user.properties file (lives under "bin" folder of your JMeter installation)

    beanshell.function.init=BeanShellFunction.bshrc
    
  2. Restart JMeter to pick the property up
  3. Once done you should be able to use it wherever required

    JMeter Beanshell Custom Function

Same approach applies to

  • beanshell.sampler.init
  • beanshell.assertion.init
  • beanshell.listener.init
  • etc.

References:

0
votes

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.

BeanShell Program

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)}"))}";

BeanShell Advance

Believe me it works and it does not give any syntax error.