2016-11-17 50 views
0

當我嘗試在BeanShell預處理器腳本中使用__FileToString內置函數時,似乎無法越過BeanShellInterpreter。 FileContents變量確實被填充,但腳本失敗,我無法使用它。下面是代碼和錯誤信息......jmeter內部函數失敗BeanSHellInterpreter

try { 
Integer count=vars.get("SessionId").length()-5; 
vars.put("vAuth", vars.get("v_username") +  vars.get("SessionId").substring(count) + ":"); 
log.info("Writing a info message"); 
    ${__FileToString(C:/tmp/DeltaConnectDemoTool_3_2_0S1_5515/bin/request.txt,,FileContents)}; 

log.info("Writing a second info message"); 
} 
catch (Throwable ex) { 
    log.error("Failed to do this or that", ex); 
} 

錯誤是

二○一六年十一月一十七日15點52分18秒錯誤 - jmeter.util.BeanShellInterpreter:錯誤調用BSH方法:eval在文件中:內聯評估:``try {Integer count = vars.get(「SessionId」)。length() - 5; vars.put(「vAuth」,vars.get ...「遇到」:「在第5行第8列。

2016/11/17 15:52:18 WARN - jmeter.modifiers.BeanShellPreProcessor:Problem在BeanShell腳本中org.apache.jorphan.util.JMeterException:調用bsh方法時出錯:eval在文件中:內聯評估:try {Integer count = vars.get(「SessionId」).length() - 5; vars。把( 「vAuth」,vars.get'遇到。 「:」 在第5行,列8

回答

0

嘗試把字符串路徑用雙引號引起來的文件,如:

${__FileToString("C:/tmp/DeltaConnectDemoTool_3_2_0S1_5515/bin/request.txt",,FileContents)}; 
0

這就是爲什麼內聯函數或不建議將變量放入腳本主體中。您有兩種備選方案:

  1. 使用「參數」部分中的功能。如果函數返回一定的價值,你將能夠在BeanShell的代碼後指在爲Parametersbsh.args[0]這樣的:

    Beanshell Parameters

  2. 實施BeanShell的代碼的功能。在你的情況下,可以FileUtils.readFileToString()功能:

    String FileContent = org.apache.commons.io.FileUtils.readFileToString(new File("request.txt")); 
    vars.put("FileContent", FileContent); 
    

How to Use BeanShell: JMeter's Favorite Built-in Component一些額外的信息,腳本在JMeter的

+0

我不認爲選擇1會爲我工作,我有多個要打開的文件(我打算爲每個RESTAPI調用一個,選項2的工作方式類似於一個魅力......謝謝!PS:文檔說在哪裏不要在腳本主體中使用內置函數(以防萬一其中有其他指針爲了我)? –