Jmeter-BeanShell-常用脚本

1.生成随机手机号

 1 //定义手机号前3位
 2 
 3 String[] FirstThreeNumber = {"134","135","136","137","138","139","147","150","152","157","158","159","172","178","182","183","184","187","188","198","130","131","132","145","155","156","166","171","175","176","185","186","166","133","149","153","173","177","180","181","189","199"};
 4 
 5 //随机获取前三位手机号的index
 6 
 7 int n= (int)(Math.random() * FirstThreeNumber.length);
 8 
 9 //获取手机前三位
10 
11 String yy = FirstThreeNumber[n];
12 
13 //循环获取手机号后8位
14 
15 for(int i = 0; i < 8; i++){
16 
17     int x = (int)(Math.random() * 9);
18 
19     yy = yy + x;
20 
21 };
22 
23 vars.put("phone", yy);
View Code

 2.生成当前时间

 1 import java.text.SimpleDateFormat;
 2 import java.util.Calendar;
 3 import java.util.Date;
 4 
 5 Date date = new Date();
 6 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 7 String nowDate = sf.format(date);
 8 Calendar cal = Calendar.getInstance();
 9 cal.setTime(sf.parse(nowDate));
10 cal.add(Calendar.DAY_OF_YEAR, +3);
11 String chanceDate = sf.format(cal.getTime());
12 cal.add(Calendar.DAY_OF_YEAR, +7);
13 String planFinishDate = sf.format(cal.getTime());
14 
15 /*当前时间*/
16 vars.put("nowDate",nowDate);
17 /*当前时间+3天*/
18 vars.put("chanceDate",chanceDate);
19 /*当前时间+3天+7天*/
20 vars.put("planFinishDate",planFinishDate);
View Code

 

posted @ 2023-07-06 15:20  路易吉  阅读(14)  评论(0编辑  收藏  举报