前言:
通常情况下,软件都是需要使用到token的,每次接口请求都携带上token证明自己的合法性。
这里我将调用注册接口,并从注册响应参数json串中提取token和userId提取出来,并通过脚本输出到txt文本中。
操作如下:
1、添加JSON提取器,并进行下图的操作:
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文件夹中。
评论区