Jmeter学习(三)–提取信息
1. Bean Shell
方式2:
通过BeanShell实现
String time = "${__time(,)}";
int [] arr = {13,15,18,17};
int index=(int)(Math.random()*arr.length);//产生0-(arr.length-1)的整数值,也是数组的索引
int rand = arr[index];
String time =time.substring(6,13);
vars.put("Phone",rand+""+${__Random(0,9,RandNo1)}+""+time+""+${__Random(0,9,RandNo2)});
//如果在测试过程中,存在重复,也可以构造其他随机数
//1476063656393
//6063697317
//063754461
//对应fastjson-1.2.68.jar
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
prev.setDataEncoding("UTF-8"); //处理乱码
String result = prev.getResponseDataAsString();
vars.put("rest",result);
//这个是获取到请求返回的数据getResponseData()针对字节;getResponseDataAsString()针对字符;,prev是获取上个请求的返回
print(result);
log.info(result);
//json格式化
JSONObject res = JSONObject.parseObject(result);
String code = res.getString("code");
print("code:"+code);
/*if(res instanceof JSONObject)
{
String code = res.getString("code");
print(code);
}
else
{
print(res);
log.info(res);
}
*/
下载文件:
import java.io.*;
byte[] result = prev.getResponseData(); //这个是获取到请求返回的数据,prev是获取上个请求的返回
String file_name = "${file_id}.pdf"; //代表存放文件的位置和文件名
File file = new File(file_name);
FileOutputStream out = new FileOutputStream(file);
out.write(result);
out.close();
2. 正则表达式
3. 断言
响应断言:对服务器的响应接口进行断言校验,来判断接口测试得到的接口返回值是否正确