jmeter 项目单接口实战记录

1.  涉及脚本机构:

  1. 测试计划--Add--配置元件:添加用户自定义变量: 设置请求url 

  2. 线程组下-add 用户beanshell 脚本,配置随机参数

import org.apache.jmeter.config.Arguments;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.text.*;

 

String firstname ="张王李赵......";
String secondname = "一二三四..............";
String halfNumber = "1234567890";

vars.put("firstname",firstname);
vars.put("second",secondname);
vars.put("halfNumber",halfNumber);

 


long id = ${__Random(100000000,999999999,id)};
String transNo = "TRNO" + id.toString();
vars.put("transNo",transNo);

 

long id = ${__Random(100000000,999999999,id)};
String channelOrgID = "C-I_D" + id.toString();
vars.put("channelOrgID",channelOrgID);
String channelOrgID = "C-I_D" + id.toString();
vars.put("channelOrgID",channelOrgID);

//创建随机时间--  时间格式:yyyyMMddHHmmss
try{
Date date = new Date(); //获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String insuredDate = sf.format(date);
Calendar cal = Calendar.getInstance(); //当前时间
cal.setTime(sf.parse(insuredDate));
cal.add(Calendar.DAY_OF_MONTH,-30);

String insuredDate = sf.format(cal.getTime());
vars.put("insuredDate",insuredDate);

}
catch(Exception e){

}

3. 请求界面的写法: 设置变量

 

4. 线程组下-设置信息头,管理接口请求的头部信息

 

4. 后置beanshell: 编写读取接口返回的参数写入到txt 中,用来执行接口关联     或者 可以采用查库的方式放入csv 中进行接口关联

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
String response_data = prev.getResponseDataAsString();
//获取登录请求的响应信息 //log.info("response_data="+response_data);
//利用上一步获取的字符串形式的JSON,结合Java处理Json的方法,将需要的键值提取出来;
JSONObject responseJson = JSON.parseObject(response_data);
String success = responseJson.getString("code"); if(success.equals("70_60_00000"))
 { // log.info("---------------------------成功返回请求数据----------------------------------");
JSONArray value_data_array = responseJson.getJSONArray("value");
JSONObject value_data_0 = value_data_array.getJSONObject(0);
String policyNo = value_data_0.getString("policyNo");
// String issuanceNo = value_data_0.getString("issuanceNo");
// String bizApplyNo = value_data_0.getString("bizApplyNo");
// log.info("policyNo="+policyNo); // log.info("issuanceNo="+issuanceNo);
// log.info("bizApplyNo="+bizApplyNo);
//保存提取的结果为res vars.put("policyNo",policyNo);
try {
   String strPath = "D:/t1/a.txt"; File file = new File(strPath);
   File fileParent = file.getParentFile();
        if(!fileParent.exists()){ fileParent.mkdirs(); }
               file.createNewFile(); FileWriter fw = new FileWriter(file, true);
               PrintWriter pw = new PrintWriter(fw);
                // log.info("csv-policyNo="+policyNo);
                // pw.println(policyNo + "\t" + vars.get("insuredDateStart") + "\t" + vars.get("insuredDateEnd") + "\t" + premium); pw.println(policyNo + "," + vars.get("channelOrgID"));
                pw.flush(); fw.flush(); fw.close();
        } catch (IOException e) {
              // TODO Auto-generated catch block e.printStackTrace();
       } }

5. 添加响应断言: 验证脚本是否执行成功,调试过程中使用,调试完毕后禁用

6. 点击运行

posted @ 2023-07-03 22:05  微风轻哨  阅读(10)  评论(0编辑  收藏  举报