JMETER的beanshell文件

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,)}

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值