1、获取时间戳:
long time = new Date().getTime();
log.info("获取手机验证码-时间戳:" + time);
String strTime =Long.toString(time);
vars.put("time", strTime);
2、生成随机手机号:
String mobile=${__Random(18000000000,18999999999,mobile)}
3、提取接口返回信息存入文件:
public void saveTokenToFile(String fileName, String content){
File file = new File(fileName);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
boolean hasFile = file.createNewFile();
if(hasFile){
log.info("file not exists, create new file");
}
fos = new FileOutputStream(file);
} else {
log.info("file exists");
fos = new FileOutputStream(file, true);
}
osw = new OutputStreamWriter(fos, "utf-8");
osw.write(content); //写入内容
log.info("mobile:"+content);
osw.write("\r\n"); //换行
} catch (Exception e) {
e.printStackTrace();
}finally { //关闭流
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
int threadNum = ${__threadNum};
log.info("当前线程id = " + threadNum);
String mobile = vars.get("mobile");
log.info("The mobile is : " + mobile);
fileName = "../"+ "tmp" + File.separator +
"jiancepingtai" + File.separator + "data" + File.separator;
log.info("fileName is : " + fileName);
saveTokenToFile(fileName+"mobile.txt", mobile);
4、设置接口返回内容编码:
prev.setDataEncoding("UTF-8")
5、提取接口返回数组列表中的某个字段重新组装成数组:
(1)提取响应数组列表
(2)String id_matchNr = vars.get("id_matchNr");
String[] array = new String[Integer.parseInt(id_matchNr)];
for(i=1;i<=Integer.parseInt(id_matchNr);i++){
array[i-1] = vars.get("id_"+i);
}
//vars.put("ids",Arrays.toString(array)); //同一线程组接口之间参数的传递
props.put("ids",Arrays.toString(array)); //不同线程组之间参数的传递
6、读取参数
(1)同一线程组之间参数的传递:
存入参数:vars.put("ids",Arrays.toString(array));
读取参数:${ids}
(2)不同线程组之间参数的传递:
存入参数:props.put("ids",Arrays.toString(array));
读取参数:${__property(ids,)}