beanshell調(diào)用jmeter 屬性常用語法
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterVariables;
LocalDateTime time = LocalDateTime.now();
String date = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//打印調(diào)試日志寫入。jmeter.log文件
log.info("----->" + date);
//****************************vars使用********************************//
//變量生成
vars.put("tmp" , date);
//變量讀取
String date1 = vars.get("tmp");
log.info("值讀取測(cè)試結(jié)果1------>" + date1);
String date = "${tmp}";
log.info("值讀取測(cè)試結(jié)果2------>" + date1);
//直接獲取jmeter自動(dòng)生成的參數(shù)變量,COOKIE__gh_sess 存放了cookies信息,是系統(tǒng)自動(dòng)添加的
log.info(vars.get("COOKIE__gh_sess"));
//操作map類型注意事項(xiàng)
HashMap map = new HashMap(5);
map.put("test1", "1");
log.info(map.toString());
//**************************ctx使用*************************************//
//參考API文檔:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
//ctx上下文信息獲取,獲取上一個(gè)請(qǐng)求result 參考API: http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
SampleResult result = ctx.getPreviousResult();
//獲取上一個(gè)請(qǐng)求的響應(yīng)信息
String code = result.getDataType();
log.info("ctx------>" + code );
//獲取Jmeter當(dāng)前運(yùn)行時(shí)變量集合。參考APIhttp://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
JMeterVariables var = ctx.getVariables();
//增加變量集合
var.put("big"," you are a big pig");
//獲取變量數(shù)據(jù),此處用法雷同vars.get();
log.info("ctx實(shí)現(xiàn)vars.get()用法------>" + var.get("COOKIE__gh_sess"));
//*********************************SampleResult 用法**************************//
//設(shè)置當(dāng)前的sampler的code
ResponseCode = 500;
//設(shè)置當(dāng)前的sampler的message
ResponseMessage = "This is a test";
//***********************prev信息*******//
String str = prev.getResponseDataAsString();
log.info("prev ------->" + str);