实现:使用jmeter,将接口返回response的“token”内容写入到excel中。
步骤:
1、要获取的http请求的response内容为:
2、选中http请求,点击鼠标右键,添加BeanShell PostProcessor
3、在jmeter安装目录lib目录下,导入需要用到的jar包。
如:本次需要用到fastjson的jar包,因此需要将【fastjson-1.2.73.jar】导入到lib目录下。
传送门:jar包下载地址:
github下载CSDN jar包下载
4、在【BeanShell PostProcessor】中写脚本
代码如下:
import com.alibaba.fastjson.JSONObject;//备注:BeanShell PostProcessor中代码如下://JMeter的内置API:prev.getResponseData()获取请求的响应内容byte[] responseData = prev.getResponseData(); //①仅以文件名作为filepath的值,则导出的文件会默认保存在Jmeter安装路径的bin(即JVM的启动路径);//private String filePath = "${ExportExcelName}";//②指定绝对路径private String filePath = "D:/token.txt"; //存放response内容的csv路径BufferedOutputStream bos = null;FileOutputStream fos = null;File file = null;JSONObject json =JSONObject.parseObject(new String(responseData));String token_=json.getJSONObject("content").getString("access_token")+",";BufferedWriter out = null;try {out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));out.write(token_+"\r\n");} catch (Exception e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}
5、运行jmeter线程组,生成的token.txt如下:
6、done
以上,生成的token.txt文件,可以用于压测参数。压测脚本避免直接生成token,尽量减轻系统对外部的依赖。
当然,生成token写入到txt/csv文件,也可通过java直接写脚本实现,本文是借助jmeter实现token的批量生成。
相关资料:
/p/4b0406795023