侧边栏壁纸
博主头像
一朵云的博客博主等级

拥抱生活,向阳而生。

  • 累计撰写 67 篇文章
  • 累计创建 25 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

JMeter -- 压测脚本之提取字段并输出文本

一朵云
2022-04-18 / 0 评论 / 3 点赞 / 8473 阅读 / 3002 字

前言:

  通常情况下,软件都是需要使用到token的,每次接口请求都携带上token证明自己的合法性。

  这里我将调用注册接口,并从注册响应参数json串中提取token和userId提取出来,并通过脚本输出到txt文本中。

image.png

操作如下:

1、添加JSON提取器,并进行下图的操作:

image.png

2、添加BeanShell 后置处理程序,通过java中文件流进行文本输出,BeanShell中添加如下内容:

	import java.io.*;
	public static void writeFile(String filePath , String info) {

	//判断文件路径中的文件夹是否存在,不存在则新增
	File file =new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
		
        FileOutputStream fps = null;
        try {
            fps = new FileOutputStream(filePath, true);
            OutputStreamWriter osw = new OutputStreamWriter(fps);
            BufferedWriter bw = new BufferedWriter(osw);
           
            bw.append(info);

            if (bw != null) {
                bw.close();
            }
            if (osw != null) {
                bw.close();
            }
            if (fps != null) {
                bw.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        //Json或正则提取器获返回
    	String info = "${token}" + "," + "${userId}\n";
    	writeFile("./out/tokens.txt",info);

  注意:这里需要注意路径,当前路径是在jmeter的安装目录下的bin文件夹中。

3、最终效果如下:

image.png

image.png

3

评论区