一、什么是Bean Shell
- BeanShell是一種完全符合Java語法規范的腳本語言,並且又擁有自己的一些語法和方法;
- BeanShell是一種松散類型的腳本語言(這點和JS類似);
- BeanShell是用Java寫成的,一個小型的、免費的、可以下載的、嵌入式的Java源代碼解釋器,具有對象腳本語言特性,非常精簡的解釋器jar文件大小為175k。
- BeanShell執行標准Java語句和表達式,另外包括一些腳本命令和語法。
二、Jmeter有哪些Bean Shell
- 定時器: BeanShell Timer
- 前置處理器:BeanShell PreProcessor
- 采樣器: BeanShell Sampler
- 后置處理器:BeanShell PostProcessor
- 斷言: BeanShell斷言
- 監聽器: BeanShell Listener
BeanShell與JMeter的關系
JMeter提供5
種方式使用BeanShell
- BeanShell Sampler
直接使用BeanShell來編寫樣例
。 - BeanShell PreProcessor
在樣例
觸發前,使用BeanShell進行加工處理。 - BeanShell PostProcessor
在樣例
觸發后,使用BeanShell進行加工處理。 - BeanShell Assertion
使用BeanShell進行斷言
處理。 - BeanShell Listener
使用BeanShell做監聽器
。
BeanShell的內置對象
對象名 | 存在元素 | 功能 |
log | BeanShell Sampler BeanShell PreProcessor BeanShell PostProcessor BeanShell Assertion BeanShell Listener |
日志信息輸出 |
Label | BeanShell Sampler | 樣例 |
FileName | BeanShell Sampler | 文件名 |
Parameters | BeanShell Sampler | 參數 |
bsh.args | BeanShell Sampler | BeanShell腳本 |
SampleResult | BeanShell Sampler BeanShell Assertion BeanShell Listener |
樣例結果 |
ResponseCode | BeanShell Sampler BeanShell Assertion |
返回的狀態碼 |
ResponseMessage | BeanShell Sampler BeanShell Assertion |
返回信息 |
IsSucess | BeanShell Sampler | 是否成功 |
ctx | BeanShell Sampler BeanShell PreProcessor BeanShell PostProcessor BeanShell Assertion BeanShell Listener |
JMeter的上下文 |
vars | BeanShell Sampler,BeanShell PreProcessor BeanShell PostProcessor BeanShell Assertion BeanShell Listener |
變量操作 |
props | BeanShell Sampler BeanShell PreProcessor BeanShell PostProcessor BeanShell Assertion BeanShell Listener |
JMeter屬性 |
prev | BeanShell PreProcessor BeanShell PostProcessor BeanShell Listener |
樣例的前置 結果讀取 |
sampler | BeanShell PreProcessor BeanShell PostProcessor |
當前樣例 |
Response | BeanShell Assertion | 返回的對象,讀-寫 |
Failure | BeanShell Assertion | 是否失敗 |
FailureMessage | BeanShell Assertion | 失敗信息 |
ResponseData | BeanShell Assertion | 返回數據體,字節形式 |
ResponseHeader | BeanShell Assertion | 返回信息頭 |
RequestHeader | BeanShell Assertion | 請求信息頭 |
SampleLabel | BeanShell Assertion | 樣例名稱 |
SampleData | BeanShell Assertion | 發送至服務器的數據 |
SampleEvent | BeanShell Listener | 讀取當前樣例的事件 |
有了上面的操作對象,可以在測試過程中,對測試的內容,進行更加詳細的
加工
。
三、BeanShell的用法
在此介紹下BeanShell PreProcessor的用法,其它的beahshell可以類推。在此我們使用beahshell調用自己寫的工具類,工具類實現了密碼的加、解密功能:
1、在eclipse寫好代碼,然后把該類打成jar包(在類上點擊右鍵->Export->jar file)
2、把jar包放到jmeter目錄\apache-jmeter-2.13\lib\ext下
3、打開jmeter,添加一個http sampler(調用登錄接口),在sampler下添加一個BeanShell PreProcessor
4、在beanshell PreProcessor中導入我們的jar包,調用里面的加、解密碼方法,把結果保存在jmeter變量中,下面兩個方法是beanshell中我們最常用到的:
vars.get(String paramStr):獲得變量值
vars.put(String key,String value):,將數據存到jmeter變量中
5、把加密后的密碼存到jmeter變量中,然后在http sampler中就可以通過${encode}進行使用了:
6、執行腳本:
四、Bean Shell常用內置變量
JMeter在它的BeanShell中內置了變量,用戶可以通過這些變量與JMeter進行交互,其中主要的變量及其使用方法如下:
log:寫入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);
ctx:該變量引用了當前線程的上下文
vars - (JMeterVariables):操作jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質上是Map),它是測試用例與BeanShell交互的橋梁,常用方法:
a) vars.get(String key):從jmeter中獲得變量值
b) vars.put(String key,String value):數據存到jmeter變量中
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");
prev - (SampleResult):獲取前面的sample返回的信息,常用方法:
a) getResponseDataAsString():獲取響應信息
b) getResponseCode() :獲取響應code
sampler - (Sampler):gives access to the current sampler
jmeter自帶函數獲取系統當前時間
//當前時間 ${__timeShift(,,,,)} //增加一天 ${__timeShift(,,P1d,,)} //增加七天時間格式化 ${__timeShift(yyyy-MM-dd,,P7d,,)} //減少七天時間格式化 ${__timeShift(yyyy-MM-dd,,-P20d,,)} //當前時間時間格式化 ${__timeShift(yyyy-MM-dd,,,,)} //增加一H ${__timeShift(,,PT1H,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,P1d,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,PT1H,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,PT10m,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,PT20S,,)} ${__timeShift(yyyy-MM-dd HH:mm:ss:SSS,,P1dT1H10m20s,,)} ${__time(YMDHMS,)} ${__time(YYYYMMDHMS,)}
浮點時間戳轉換為標准時間
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; //10位的秒級時間戳 long time1 = ${time}; //獲取時間戳變量 String result1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new Date(time1 * 1000 )); log.info( "10位時間戳(秒)--->Date:" +result1); //13位的毫秒級時間戳 //double time2 = 1515730332000d; //String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2); //System.out.println("13位數的時間戳(毫秒)--->Date:" + result2);
解析jsonlist
//利用beanshell獲取到json響應,而后經過JSONObject 和JSONArray 將數組解析,遍歷數組的length以后,提取參數值 //導入json包 import org.json.*; //獲取獲取請求的返回值 String response_data = prev.getResponseDataAsString(); //日志打印獲取請求的返回值 log.info(response_data); //將String類型的返回值構形成JSONObject對象 JSONObject data_obj = new JSONObject(response_data); //獲取做為下一個請求post的參數值Province(兩種方式) //String Provincelist_str = data_obj.get("Province").toString(); JSONArray Provincelist_str = data_obj.getJSONArray( "Province" ); //log.info(Provincelist_str); //獲取Province數組的長度 int len = Provincelist_str.length(); String strlen = Integer.toString(len); vars.put( "MessageNum" ,strlen); log.info(strlen); int i = 0 ; for (;i < len;++i) { //獲取 data[ i ] 數組對象 JSONObject jsonTemp = (JSONObject)Provincelist_str.getJSONObject(i); switch (i) { case 0 : //兩種提取參數的寫法 String NameItems = jsonTemp.getString( "Name" ); // String NameItems = jsonTemp.get("Name").toString(); // 兩種打印參數的方法 // vars.put("Name_1", jsonTemp.getString("Name")); vars.put( "Name_1" , NameItems); log.info(NameItems); } }