jmeter 使用BeanShell后处理器时csv文件为空

z6psavjg  于 2022-11-09  发布在  Shell
关注(0)|答案(4)|浏览(517)

我正在尝试使用BeanShell后处理器将变量保存到csv文件中,代码:

String id = "${userID}";
FileWriter fstream = new FileWriter("JmeterBean.csv",true);
fstream.write(id+"\n");
fstream.close();

测试计划:

  1. HTTP请求GetUsersById =〉返回所有ID
  • Json提取器=〉来自我的响应
{"@class":"com.test.dto.userDTO",
      "author":"John",
      "id":"89BC331D723F",  },

{"@class":"com.test.dto.userDTO",
      "author":"Alex",
      "id":"FTH7JBDRF567",
   }
  • 变量名称:使用者ID
  • JSON路径表达式:$.[?(@.@class=='com.test.dto.userDTO')].id
  • 匹配号码:-1
  • BeanShell后处理器

但我的csv文件总是空的,看起来像这样:

pu3pd22g

pu3pd22g1#

使用vars获取变量

String id = vars.get("userID");

vars -(JMeterVariables)-提供对变量的读/写访问权限:vars.get(key);
并且更喜欢使用JSR223 PostProcessor而不是Beanshell PostProcessor

pgx2nnw8

pgx2nnw82#

String id  = vars.get("userID");
FileWriter fstream = new FileWriter("JmeterBean.csv",true);
fstream.write(id+"\n");
fstream.close();

我这样做了,但我得到了相同的结果与null在我的csv文件:

xpcnnkqh

xpcnnkqh3#

1.如果JSON Extractor生成多个匹配项,则您没有userID变量,您将得到如下结果:

userID_1=89BC331D723F
userID_2=FTH7JBDRF567
userID_matchNr=2

因此我建议仔细检查哪些JMeter Variables是由JSON提取器使用Debug Sampler and View Results Tree listener组合生成的。
1.从JMeter3.1开始,建议使用JSR223测试元素和Groovy语言编写脚本
假设以上都是,添加JSR223 PostProcessor(确保它位于JSON Extractor之后)并使用以下代码:

1.upto(vars.get('userID_matchNr') as int, { number ->
    new File('JmeterBean.csv') << vars.get('userID_' + number) << System.getProperty('line.separator')
})
bweufnob

bweufnob4#


# The below mentioned solution worked for me:

OrderID = vars.get("primaryRefID");
FileWriter fstream = new 
FileWriter("C://Users/Documents/JMeter/OrderID.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("TC-"+OrderID);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

相关问题