一、log 對象
寫入信息到jmeber.log文件,使用方法:log.info(“Agoly”);
二、 ctx對象
該變量引用了當前線程的上下文,使用方法可參考:org.apache.jmeter.threads.JMeterContext。
三、 vars對象
操作jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質上是Map),它是測試用例與BeanShell交互的橋梁,常用方法:
a) vars.get(String key):從jmeter中獲得變量值
b) vars.put(String key,String value):數據存到jmeter變量中
更多方法可參考:org.apache.jmeter.threads.JMeterVariables
四、props 對象
-
props - (JMeterProperties - class java.util.Properties):操作jmeter屬性,該變量引用了JMeter的配置信息,可以獲取Jmeter的屬性,它的使用方法與vars類似,但是只能put進去String類型的值,而不能是一個對象。對應於java.util.Properties。
a) props.get("START.HMS"); 注:START.HMS為屬性名,在文件jmeter.properties中定義
b) props.put("PROP1","1234");
五、SampleResult 對象(只能用於Bean Shell 斷言)
獲取前面的sample返回的結果,常用方法:
String responseData= SampleResult.getResponseDataAsString(); //獲取響應數據
String responseCode = SampleResult.getResponseCode(); //獲取響應碼 HTTP: 200 、502、404等
String sampleLabel=SampleResult.getSampleLabel(); //接口名稱
String url = SampleResult.getUrlAsString() ; //請求url
String samplerData = SampleResult.getSamplerData() ; //請求數據 ;請求url、請求body
String requestHeaders= SampleResult.getRequestHeaders() ; // 請求header
boolean status = SampleResult.isResponseCodeOK(); // HTTP 返回 200時為true
更多方法可參考:org.apache.jmeter.samplers.SampleResult
六、 prev對象(用於后置處理器、斷言)
實際是一個SampleResult對象;使用同上
SampleResult 與prev 區別: 前者只能用於Bean Shell 斷言,后者可以用於后置處理器、斷言
七、samper對象(用於前置處理器)
-
sampler (是Samper對象) 參考http://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html
a)獲取http請求的 url
String url = sampler.getPath();
b)獲取http請求的body內容
Arguments arguments = sampler.getArguments(); // 調用時注意sampler小寫
String body = arguments.getArgument(0).getValue();
備注:需要引入包import org.apache.jmeter.config.Arguments;
八、beanshell斷言
Failure = true;
FailureMessage = "斷言失敗描述";
九、BeanShell的API文檔
http://jmeter.apache.org/api/