import com.alibaba.fastjson.serializer.SerializerFeature;
import java.time.format.DateTimeFormatter;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import java.math.BigDecimal;
import java.time.LocalDate;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
//请求头的增删改????????
String token=vars.get("token");
sampler.getHeaderManager().add(new Header("X-ER-UAT",token));
sampler.getHeaderManager().removeHeaderNamed("X-ER-UAT");
//结果状态处理
if( !((SampleResult.getResponseCode()).equals("200")) )
{
ctx.getThread().setOnErrorStartNextLoop(true);//ResponseCode响应码
}
if( !((vars.get("status")).equals("200")) )
{
ctx.getThread().setOnErrorStartNextLoop(true);//响应数据中的状态码
}
//自动切换测试环境的数据
String host=sampler.getDomain();//运行IP
String HOST_D=vars.get("HOST_D");//IP1
String HOST_Y=vars.get("HOST_Y");//IP2
if(HOST_D.equals(host)){}//获取IP1环境的测试数据
if(HOST_Y.equals(host)){}//获取IP2环境的测试数据
//jmeter日期格式化
${__timeShift(yyyyMMdd,,,,)}
${__timeShift(YYYYMMddHHmmssSSS,,,,)}
${__timeShift(yyyy-MM-dd HH:mm:ss,,P5D,,)}
//java日期格式化
//jdk8之前
//jdk8
LocalTime currentTime =LocalTime.now();
LocalDate currentDate =LocalDate.now();
LocalDateTime currentDateTime=LocalDateTime.now();
System.out.println(currentTime.toString());
System.out.println(currentDate.toString());
System.out.println(currentDateTime.toString());
//日期加减
String strOldDate=jsonObj.get("oldDate");
LocalDate date=LocalDate.parse(strOldDate,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate newDate =date.plusMonths(24);
String strNewDate= newDate.toString();
//金额加减
String strOldMoney=vars.get("oldMoney");
BigDecimal bigDecimalOldMoney=new BigDecimal(strOldMoney);
BigDecimal bigDecimalNewMoney=bigDecimalOldMoney.add(new BigDecimal(1.00));
String strNewMoney=bigDecimalNewMoney.toString();
//字符串转数字
int intNum=Integer.parseInt("");
//字符串截取
String strNew=data0.substring(0, data0.length()-1);
String strNew=data0.substring(0, data0.indexOf("CheckNode"));
String strNew=data0.substring(0, data0.lastIndexOf("CheckNode"));
//字符串拼接
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("taskType=");
stringBuffer.append("&formData=undefined");
String data=stringBuffer.toString();
vars.put("data",data);
//字符串替换
String data1=data0.replace("{","").replace("}","");
String data2=data1.replace("\":\" \"","=").replace("\":\"\"","=").replace("\":","=");
String data3=data2.replace("null","").replace("\"","");String data4=data3.replace(",","&");
String data=data4+append;
vars.put("submitData",data);
//JSONObject
JSONObjectString strData=vars.get("data_getData");
JSONObject json_data=JSON.parseObject(strData);
JSONObject json_data=new JSONObject();
//JSON数据的增删改
json_data.put("newDate",strNewDate);
json_data.put("newMoney",strNewMoney);
json_data.remove("taskId");
json_data.replace("taskId",taskId);
json_data.replace("money",json_data.getLongValue("money"));//解决科学计数法
//JSONArray
JSONArray jsonArray_data=json_data.getJSONArray("JSONArrayData");
int L=jsonArray_data.size();
for (int i = 0; i < L; i++)
{
JSONObject json_temp=jsonArray_data.getJSONObject(i);
int intValue=json_temp.getIntValue("IntValue");
double dd =jsonTemp.getDoubleValue("dd");
if(dd >0.00)
{
String aa =jsonTemp.getString("aa");
vars.put("aa",aa);
String bb =jsonTemp.getString("bb");
vars.put("bb",bb);
L=0;//结束循环
}
}
//解决json数据中空值或null值参数消失问题
SerializerFeature[] serializerFeatures={SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullBooleanAsFalse,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullStringAsEmpty};
String data0=JSON.toJSONString(json_data,serializerFeatures);
//数据编码1
String encodeData=${__urlencode(data)};
vars.put("encodeData",encodeData);
//数据编码2
try {
encodeData = URLEncoder.encode(data,"utf-8");
//String encodeData=java.net.URLEncoder.encode("","utf-8");
//String decoderData=java.net.URLDecoder.decode("","utf-8");
} catch (UnsupportedEncodingException e)
{
System.out.println("====URLEncoder.encode.error===="+e.getMessage());
}
vars.put("encodeData",encodeData);
/*------------------------------------------------------------------*
//正则表达式提取器
Set-Cookie: JSESSIONID=(.*); Path=/; HttpOnly ------- $1$
Set-Cookie: token=(.*); expires=(.*); path=/
Set-Cookie: ls=(.*)=; expires=(.*); path=/
Set-Cookie: pwd=(.*); expires=(.*); path=/
Set-Cookie: desCode=(.*); expires=(.*); path=/
//JSON提取器:一对一提取
$.data.grid.FF $..FF
$.data.grid.list[*].FF $..FF
$.data.grid.list[?(@.A=="${a}")].FF $..[?( @.A == "${a}" && @.A != "${a}" || @.A == "${a}")].FF
//JSON提取器:一对多提取
变量名称: A;B;C;D
提取表达式: $.data.A;$.data.B;$.data.C;$.data.D
匹配项: 1;1;1;1
默认值: null;null;null;null
/*------------------------------------------------------------------*/
//命令行方式执行JMX脚本
jmeter -n -t [脚本路径].jmx -l [执行结果路径].jtl -e -o D:\120
//WebUI
//*[@class="content"]
//*[@id="pageContainer"]