2017-07-07 189 views
1

預備知識:呼叫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

回答

0

從這個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
  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; –