无法使用jmeter中的beanshell后处理器在csv文件中写入surevyId。空值而不是SurveyId

问题描述 投票:0回答:1
{
"code": 200,
"surveyId": 3239640,
"success": 1
}

我想在csv文件中写入SurveyId,但是正在写入null而不是SurveyId。我编写了如下的beanshell脚本:

vars.put ("Response Data",ctx.getPreviousResult().getResponseDataAsString());

log.info(vars.get("Response Data"));

try{

String surveyId = vars.get("surveyId");

}

catch(Exception ex) { log.warn("Error in my script", ex); throw ex; } log.info("surveyId is " +surveyId); f = new FileOutputStream("C://Users//garim//Downloads//apache-jmeter-5.1.1//apache-jmeter-5.1.1//bin//SurveyId.csv", true); p = new PrintStream(f); this.interpreter.setOut(p); print(surveyId); f.close();
jmeter beanshell
1个回答
0
投票
  1. 您的surveyId变量未在脚本中的任何位置定义,因此为空,您需要使用JSON Extractor来获取它,并且JSON Extractor必须是before Beanshell PostProcessor

    ] >>
  2. 一般来说,since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language用于脚本编写,给定Groovy has built-in JSON support,您可以将代码修改为:]]

    String surveyId = (new groovy.json.JsonSlurper().parse(prev.getResponseDataAsString()).surveyId) as String
    new File("C://Users//garim//Downloads//apache-jmeter-5.1.1//apache-jmeter-5.1.1//bin//SurveyId.csv") << surveyId << System.getProperty("line.separator")
    
  3. 更多信息:Apache Groovy - Why and How You Should Use It

© www.soinside.com 2019 - 2024. All rights reserved.