Jmeter BeanShell采样器提取接口响应写入csv文件(四)

 

1、调用登录接口获取response数据

{"code":0,"msg":"成功","data":{"token":"bearereyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9tZW1iZXItYXBpLnN0MS50ZXN0LmxhbnhpbmthLmNvbVwvMi4wXC91c2Vyc1wvbG9naW4iLCJpYXQiOjE1ODYzMTEyMDUsImV4cCI6MTU4NzYwNzIwNSwibmJmIjoxNTg2MzExMjA1LCJqdGkiOiIyWWJGOTNVaVJhSDNKODkzIiwic3ViIjo1ODQ5MDIsInBydiI6IjNhN2IwNmU5NTBkMDhlMjMzMjkyMjdjN2E2YTUyMzQyYWJiNGYxOWIiLCJidXNpbmVzc190eXBlIjoiNiJ9.wrZkzkZKHoBj_cAxQzELJpm2VPWYQ"}}

2、提取response中的数据并写入到csv中

//导入json包
import com.alibaba.fastjson.JSONObject;
//获取获取请求的返回值
String response_data = prev.getResponseDataAsString();
//日志打印获取请求的返回值
log.info(response_data);
//将String类型的返回值构造成JSONObject对象
JSONObject data_obj = JSONObject.parseObject(response_data);
String msg = data_obj.get("msg").toString();  //取出“msg”的值
String token = data_obj.get("data").get("token").toString();  //取出“token”的值
log.info(msg);
log.info(token);

//写入文件
FileWriter fstream=new FileWriter("C:/jmeter/apache-jmeter-5.0/bin/TestResult/pc.csv",true);//本地存储文件路径
BufferedWriter out = new BufferedWriter(fstream);
out.write(msg);//提取出来的msg
out.write(",");//换列
out.write(token);//提取出来的token
out.write(System.getProperty("line.separator"));//换行
out.close();
fstream.close();

3、存入csv中数据文件

Jmeter BeanShell采样器提取接口响应写入csv文件(四)_第1张图片

 

 

你可能感兴趣的:(jmeter,jmeter,BeanShell,取样器)