如何使用jmeter BeanShell替换反斜杠(\)以转发斜杠(/)

时间:2018-04-06 19:32:55

标签: java groovy jmeter beanshell

我在BeanShell下面使用BeanShell将用户定义的变量声明为'projectHome',它返回jmx文件的绝对路径。

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}

在Windows上返回:projectHome = C:\ Users \ mdwivedi \ Desktop \ API_Testing

在MacOS上返回: PROJECTHOME = /用户/ mdwivedi /桌面/ API_Testing

变量值在 MacOS 上的以下BeanShell Sampler中正常工作:

import java.io.FileWriter;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;
//Default separator
char SEPARATOR = ',';
//function write line in csv
public void writeLine(FileWriter writer, String[] params, char separator) {
    boolean firstParam = true;
    StringBuilder stringBuilder = new StringBuilder();
    String param = "";
    for (int i = 0; i < params.length; i++) {
        //get param
        param = params[i];
        log.info(param);
        //if the first param in the line, separator is not needed
        if (!firstParam) {
            stringBuilder.append(separator);
        }
        //Add param to line
        stringBuilder.append(param);
        firstParam = false;
    }
    //prepare file to next line
    stringBuilder.append("\n");
    //add to file the line
    log.info(stringBuilder.toString());
    writer.append(stringBuilder.toString());
}
//get path of csv file (creates new one if its not exists)
String csvFile = "${projectHome}/tenant_details.csv"; 
String[] params = {"${Email}"};
FileWriter fileWriter = new FileWriter(csvFile, true);
writeLine(fileWriter, params, SEPARATOR);
//proper close to file
fileWriter.flush();
fileWriter.close();

当我在Windows机器上运行脚本时,由于路径有反斜杠,它失败了。

如何在同一个BeanShell Sampler中将反斜杠转换为正斜杠,以便它可以在Windows和MacOS上运行?

4 个答案:

答案 0 :(得分:0)

在beanshell Sampler / Post-PreProcessor中,将脚本文件夹和脚本名称的路径输入到“脚本文件”字段中,如下所示: -

enter image description here

$ {__ BeanShell(File.separator,)}将返回Unix中的/(Mac,Linux,...)和Windows中的\。所以你可以把它看作:

- Windows:$ {PATH} \ Foldername \ beanshellfile.bsh

- Unix:$ {PATH} /scripts/beanshellfile.bsh

答案 1 :(得分:0)

在Sample Sampler中,您可以在需要添加时使用相关操作系统文件分隔符调用File.separator。在您的代码中更改为:

 String csvFile = "${projectHome}" + File.separator + "tenant_details.csv"; 

答案 2 :(得分:0)

  1. 您无需将斜杠/替换为反斜杠\,斜杠也将适用于Windows平台,您可以执行以下操作:

    new File("c:/Windows/system32/cmd.exe")
    

    ,它将正常解决。

  2. 不要在脚本中引用${projectHome}和/或${Email}之类的变量,而应使用vars的简写形式。它代表JMeterVariables类实例,并提供对范围内所有JMeter变量的读/写访问。所以你应该使用

    String csvFile = vars.get("projectHome") + "/tenant_details.csv";
    

    String[] params = {vars.get("Email")};  
    
  3. JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for scripting起,请考虑在下一个可用机会上迁移到Groovy,因为Beanshell(并且将)在高负载时成为性能瓶颈。有关更多信息,请参见Apache Groovy - Why and How You Should Use It文章。

答案 3 :(得分:0)

这是我使用BeanShell解决此问题的方法:

import org.apache.jmeter.services.FileServer;
String JMXPath = FileServer.getFileServer().getBaseDir();
char[]a = JMXPath.toCharArray();
log.info(JMXPath + " - It is project JMX path");
    for (int i = 0; i < a.length; i++)
            {
                if (a[i] == '\\')
                {
                a[i] = '/';
                }
            }
String xyz = String.valueOf(a);

log.info(xyz + " - It is projectHome value");
vars.put("projectHome", xyz);