呼叫BeanShell的功能等
预备知识:呼叫BeanShell的功能等
内部JMeter的bin文件夹,我已经编辑BeanShellFunction.bshrc
文件来添加我的函数如下
String getMyString()
{
return "MyString";
}
我已启用BeanShellFunction.bshrc
从jmeter.properties
文件为
beanshell.function.init = BeanShellFunction.bshrc
当我使用以下语法来调用函数时,它工作正常。
${__BeanShell(getMyString())}
问:
我如何可以调用从BeanShell的程序,如预处理器,PostProcessor中断言等同样的功能?
分析:
我试着用以下,但没有运气:
String myStr = getMyString();
它给出了一个错误为:
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()
从这个SO后我找到了解决办法:Calling Jmeter Functions from BeanShell Assertion Script
解决方案
˚F或每个BeanShell的节目类型有不同在bin/user.properties
定义beanshell.*.init
属性:
beanshell.function.init=BeanShellFunction.bshrc
beanshell.preprocessor.init=BeanShellSampler.bshrc beanshell.postprocessor.init=BeanShellSampler.bshrc beanshell.assertion.init=BeanShellFunction.bshrc
因此需要被从任何程序(预处理,后处理器,等等),我们需要称为相同功能的功能复制到每.bshrc
文件或使用相同.bshrc
文件为每个程序init属性。
使用语法:
您需要使用用于发送URL参数相同的语法:
String myStr = "${__BeanShell(getMyString())}";
这会自动调用从定义.bshrc
文件BeanShell的方法。
对于高级脚本
如果您的BeanShell函数接受一个参数:
String getMyString(String strParam)
{
return "MyString: "+strParam;
}
而你要一个属性作为参数传递给BeanShell的功能,您可以使用以下语法:
String myStr = "${__BeanShell(getMyString("${__P(param1)}"))}";
相信我它的工作原理,它不会给出任何语法错误。
-
在下一行添加到user.properties文件(生活在你安装Jmeter的 “bin” 文件夹)
beanshell.function.init=BeanShellFunction.bshrc
- 重启JMeter的挑财产高达
-
一旦完成你应该可以在任何需要的地方使用它
同样的方法适用于
beanshell.sampler.init
beanshell.assertion.init
beanshell.listener.init
- 等
参考文献:
您不明白我的问题,请您详细说明您的第3点。在BeanShell程序中使用它的语法是什么? –
1.将'$ {__ BeanShell(getMyString())}'放到参数部分 2.将它作为String str = Parameters; –