1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Jmeter将接口response内容写入到excel

Jmeter将接口response内容写入到excel

时间:2022-03-17 13:18:19

相关推荐

Jmeter将接口response内容写入到excel

实现:使用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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。