呼叫BeanShell的功能等

呼叫BeanShell的功能等

问题描述:

预备知识:呼叫BeanShell的功能等

内部JMeter的bin文件夹,我已经编辑BeanShellFunction.bshrc文件来添加我的函数如下

String getMyString() 
{ 
    return "MyString"; 
} 

我已启用BeanShellFunction.bshrcjmeter.properties文件为

beanshell.function.init = BeanShellFunction.bshrc

当我使用以下语法来调用函数时,它工作正常。

${__BeanShell(getMyString())} 

它工作正常的情况如下:
BeanShell Function Call

问:

我如何可以调用从BeanShell的程序,如预处理器,PostProcessor中断言等同样的功能?

分析:

我试着用以下,但没有运气:

String myStr = getMyString(); 

BeanShell Assertion

它给出了一个错误为:

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

从这个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 Program

对于高级脚本
如果您的BeanShell函数接受一个参数:

String getMyString(String strParam) 
{ 
    return "MyString: "+strParam; 
} 

而你要一个属性作为参数传递给BeanShell的功能,您可以使用以下语法:

String myStr = "${__BeanShell(getMyString("${__P(param1)}"))}"; 

BeanShell Advance

相信我它的工作原理,它不会给出任何语法错误。

  1. 在下一行添加到user.properties文件(生活在你安装Jmeter的 “bin” 文件夹)

    beanshell.function.init=BeanShellFunction.bshrc 
    
  2. 重启JMeter的挑财产高达
  3. 一旦完成你应该可以在任何需要的地方使用它

    JMeter Beanshell Custom Function

同样的方法适用于

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

参考文献:

+0

您不明白我的问题,请您详细说明您的第3点。在BeanShell程序中使用它的语法是什么? –

+0

1.将'$ {__ BeanShell(getMyString())}'放到参数部分 2.将它作为String str = Parameters; –