首页 > 解决方案 > 在 JMeter 中使用 Beanshell 采样器将变量写入 txt/csv 文件

问题描述

我的要求是使用在不同步骤中使用的 2 个 Beanshell 采样器写入两个值,在一行中并用逗号分隔但第二个变量写在新行上

我在不同的步骤中有两个不同的 Beanshell 采样器。第一个捕获变量 1 并将其写入文件 第二个捕获变量 2 并将其写入文件

第一个代码:

String path= FileServer.getFileServer().getBaseDir() + "//P_IssuePolicy.txt";
SubmissionNum= vars.get("CP_SubmissionNumber");
EMailID= vars.get("P_emailID");
f = new FileOutputStream(path, true);
p = new PrintStream(f);
this.interpreter.setOut(p); 
p.println(EMailID+","+SubmissionNum);
f.close();

第二个代码:

String path= FileServer.getFileServer().getBaseDir() + "//P_IssuePolicy.txt";
Policynumber= vars.get("CP_Policynumber");
f = new FileOutputStream(path, true); 
p = new PrintStream(f);
this.interpreter.setOut(p);
p.println(","+Policynumber);
f.close();

预期结果:

 abc@email.com,12345601,12345602

实际结果:

 abc@email.com,12345601

 ,12345602

标签: jmeterbeanshell

解决方案


而不是println添加新行使用打印

 p.print(EMailID+","+SubmissionNum);

推荐阅读