原文鏈接:https://www.cnblogs.com/zgq123456/p/9782202.html
說明:本文部分資料摘抄至
- 來源: http://www.cnblogs.com/puresoul/p/4915350.html
- 來源: http://www.cnblogs.com/puresoul/p/4949889.html
- 來源: http://blog.csdn.net/silencemylove/article/details/51373873
一、什么是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的用法
在此介紹下BeanShell PreProcessor的用法,其它的beahshell可以類推。在此我們使用beahshell調用自己寫的工具類,工具類實現了密碼的加、解密功能:
1、在eclipse寫好代碼,然后把該類打成jar包(在類上點擊右鍵->Export->jar file)ar包放到jmeter目錄\apache-jmeter-2.13\lib\ext下
3、打開jmeter,添加一個http sampler(調用登錄接口),在sampler下添加一個BeanShell PreProcessor(如果jmeter已經打開了,步驟2中jar包要生效,必須才重啟jmeter)
4、在beanshell PreProcessor中導入我們的jar包,調用里面的加、解密碼方法,把結果保存在jmeter變量中,下面兩個方法是beanshell中我們最常用到的:
- vars.get(String paramStr):獲得變量值
- vars.put(String key,String value):,將數據存到jmeter變量中
import com.pingan.ff.account.user.utils.*; //加密 System.out.println("*****加密*****"); String password = "123123"; String encode = SecurityUtils.getKey(password);//調用工具類中的方法進行加密 System.out.println("Set my encode"); vars.put("encode",encode);//把值保存到jmeter變量encode中 String getEncode=vars.get("encode"); System.out.println("Get my encode: " + getEncode);
5、把加密后的密碼存到jmeter變量中,然后在http sampler中就可以通過${encode}進行使用了:
6、執行腳本:
四、Bean Shell常用內置變量
JMeter在它的BeanShell中內置了變量,用戶可以通過這些變量與JMeter進行交互,其中主要的變量及其使用方法如下:
-
log:寫入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);
-
ctx:該變量引用了當前線程的上下文,使用方法可參考:org.apache.jmeter.threads.JMeterContext。
-
vars - (JMeterVariables):操作jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質上是Map),它是測試用例與BeanShell交互的橋梁,常用方法:
a) vars.get(String key):從jmeter中獲得變量值
b) vars.put(String key,String value):數據存到jmeter變量中
更多方法可參考:org.apache.jmeter.threads.JMeterVariables
-
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
更多方法可參考:org.apache.jmeter.samplers.SampleResult
-
sampler - (Sampler):gives access to the current sampler
五、自定義函數
在BeanShell中,我們可以使用java語言自定義函數來處理特定的邏輯,結合BeanShell的內置對象進行變量的存取,方便我們進行測試提高腳本的靈活性。
示例:
1、在Test Plan中添加一個變量:hello = kitty
2、Debug sampler-1和Debug sampler-2什么都不處理,用來查詢對比beahshell處理前后的結果
3、BeanShell Sampler中的腳本如下:
4、運行結果:
- Debug sampler-1中顯示:hello=kitty
- BeanShell sampler中 返回結果為:success
- Debug sampler-1中顯示:hello=world,jmeter=111111
有沒有覺得上面(三)中自定義函數這樣的方式太麻煩並且也不美觀?而且如果我們已經有現成的java源文件或者class文件時,我們有沒有什么辦法直接在jemter中引用?這就是這部分要介紹的內容,直接上示例:
1、假如我有一個java 源文件,名為:Myclass.java,代碼如下:
package test; public class Myclass { public int add(int a, int b) { return a + b; } }
2、Bean Shell使用代碼如下:
在bean shel中通過source("代碼路徑")方法引入java,然后調用方法和java一樣,new一個class,再調用里面的add 方法。
3、運行結果:
七、引用外部class文件
現在知道如何引用外部文件,有時候如果我們只有class文件怎么辦呢?其實在jmeter中也可以直接引用class文件,示例如下:
1、直接把上例中的java文件編譯成class文件,如何編譯請自行百度。
2、Bean Shell使用代碼如下:
用addClassPath("D:\\")方法引入 class文件,在用import導入包及類,然后就可以像java一樣調用了
3、運行結果:
八、引用外部Jar包
上面四、五介紹了如何引用外部java和class文件,如果文件比較多時我們可以把它們打成一個jar包然后在jemter中調用,具體如何使用可以看我上一篇有介紹:Jmeter之Bean shell使用(一)。
在這里想補充一點的是jmeter中引入jar的方法:
1、上一篇中已使用過的:把jar包放到jmeter目錄\apache-jmeter-2.13\lib\ext下
2、在Test Plan的右側面板最下方直接添加需要引用的jar包,如下圖:
九、其它用法
1、在Test Plan中定義如下三個變量:
2、Bean Shell可腳本如下:
a、bean shell可以接受傳入參數,如下圖:${u1} ${u2} ${u3}
b、參數可以通過bsh.args[]按順序提取
c、bean shell提供了一個內置變量Parameters,來保存參數的集合
3、運行結果:
下圖中1輸入的這兩句設置:
ResponseCode = 500;
ResponseMessage = "This is a test";
下圖中2輸入的這兩句設置:
log.info(Parameters);
log.info(Label);
十、提取json數據
需求:提取sample返回json數據中所有name字段值,返回的json格式如下:
{“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}}
jmeter中添加后置處理器BeanShell PostProcessor
說明:腳本中的導入的json包需要自己去網絡下載后放到\lib\ext,請悉知
import org.json.*; String response_data = prev.getResponseDataAsString(); JSONObject data_obj = new JSONObject(response_data); String apps_str = data_obj.get("body").get("apps").toString(); JSONArray apps_array = new JSONArray(apps_str); String[] result = new String[apps_array.length()]; for(int i=0;i<apps_array.length();i++){ JSONObject app_obj = new JSONObject(apps_array.get(i).toString()); String name = app_obj.get("name").toString(); result[i] = name; } vars.put("result", Arrays.toString(result));
十一、斷言
import txtWrite.*; String response_data = prev.getResponseDataAsString(); String assert_data="科目${num}"; TxtWrite writeData=new TxtWrite(); if(response_data.indexOf(assert_data)!=-1)//請求的response中有包含自定義字符,則斷言結果為false { Failure=false; String message="${__time(yyyy-MM-dd HH:mm:ss,)} 新建科目【"+assert_data+"】成功"; FailureMessage=message; writeData.contentToTxt("D:/xykyInterfaceAutoTest.log",message); }else { Failure=true; String message="${__time(yyyy-MM-dd HH:mm:ss,)} 新建科目【"+assert_data+"】失敗----------"; String response="本次請求response數據:"+prev.getResponseDataAsString()+"----------"; String need_assert="需要斷言的數據:"+assert_data; FailureMessage=message+response+need_assert; writeData.contentToTxt("D:/xykyInterfaceAutoTest.log",message+response+need_assert); }