一、什么是Bean Shell
官網(wǎng):http://www.BeanShell.org/
二、Jmeter有哪些Bean Shell
定時器: BeanShell Timer
前置處理器:BeanShell PreProcessor
采樣器: BeanShell Sampler
后置處理器:BeanShell PostProcessor
斷言: BeanShell斷言
監(jiān)聽器: BeanShell Listener
三、BeanShell的用法
在此介紹下BeanShell PreProcessor的用法,其它的beahshell可以類推。在此我們使用beahshell調(diào)用自己寫的工具類,工具類實現(xiàn)了密碼的加、解密功能:
1、在eclipse寫好代碼,然后把該類打成jar包(在類上點擊右鍵->Export->jar file)
2、把jar包放到j(luò)meter目錄\apache-jmeter-2.13\lib\ext下
3、打開jmeter,添加一個http sampler(調(diào)用登錄接口),在sampler下添加一個BeanShell PreProcessor
4、在beanshell PreProcessor中導(dǎo)入我們的jar包,調(diào)用里面的加、解密碼方法,把結(jié)果保存在jmeter變量中,下面兩個方法是beanshell中我們最常用到的:
import com.pingan.ff.account.user.utils.*;//加密System.out.println("*****加密*****");String password = "123123";String encode = SecurityUtils.getKey(password);//調(diào)用工具類中的方法進(jìn)行加密System.out.println("Set my encode");vars.put("encode",encode);//把值保存到j(luò)meter變量encode中String getEncode=vars.get("encode");System.out.println("Get my encode: " + getEncode);
5、把加密后的密碼存到j(luò)meter變量中,然后在http sampler中就可以通過${encode}進(jìn)行使用了:
6、執(zhí)行腳本:
四、Bean Shell常用內(nèi)置變量
JMeter在它的BeanShell中內(nèi)置了變量,用戶可以通過這些變量與JMeter進(jìn)行交互,其中主要的變量及其使用方法如下:
log:寫入信息到j(luò)meber.log文件,使用方法:log.info(“This is log info!”);
ctx:該變量引用了當(dāng)前線程的上下文,使用方法可參考:org.apache.jmeter.threads.JMeterContext。
vars - (JMeterVariables):操作jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質(zhì)上是Map),它是測試用例與BeanShell交互的橋梁,常用方法:
a) vars.get(String key):從jmeter中獲得變量值
b) vars.put(String key,String value):數(shù)據(jù)存到j(luò)meter變量中
更多方法可參考:org.apache.jmeter.threads.JMeterVariables
props - (JMeterProperties - class java.util.Properties):操作jmeter屬性,該變量引用了JMeter的配置信息,可以獲取Jmeter的屬性,它的使用方法與vars類似,但是只能put進(jìn)去String類型的值,而不能是一個對象。對應(yīng)于java.util.Properties。
a) props.get("START.HMS"); 注:START.HMS為屬性名,在文件jmeter.properties中定義
b) props.put("PROP1","1234");
prev - (SampleResult):獲取前面的sample返回的信息,常用方法:
a) getResponseDataAsString():獲取響應(yīng)信息
b) getResponseCode() :獲取響應(yīng)code
更多方法可參考:org.apache.jmeter.samplers.SampleResult
sampler - (Sampler):gives access to the current sampler
一、操作變量
二、操作屬性
三、自定義函數(shù)
四、引用外部java文件
五、引用外部class文件
六、引用外部Jar包
七、其它用法(接受參數(shù), log等)
一、操作變量:通過使用Bean shell內(nèi)置對象vars可以對變量進(jìn)行存取操作
a) vars.get("name"):從jmeter中獲得變量值
b) vars.put("key","value"):數(shù)據(jù)存到j(luò)meter變量中
二、操作屬性:通過使用Bean shell內(nèi)置對象props 可以對屬性進(jìn)行存取操作
a) props.get("START.HMS"); 注:START.HMS為屬性名,在文件jmeter.properties中定義
b) props.put("PROP1","1234");
三、自定義函數(shù):
在BeanShell中,我們可以使用java語言自定義函數(shù)來處理特定的邏輯,結(jié)合BeanShell的內(nèi)置對象進(jìn)行變量的存取,方便我們進(jìn)行測試提高腳本的靈活性。
示例:
1、在Test Plan中添加一個變量:hello = kitty
2、Debug sampler-1和Debug sampler-2什么都不處理,用來查詢對比beahshell處理前后的結(jié)果
3、BeanShell Sampler中的腳本如下:
4、運行結(jié)果:
四、引用外部java文件:
有沒有覺得上面(三)中自定義函數(shù)這樣的方式太麻煩并且也不美觀?而且如果我們已經(jīng)有現(xiàn)成的java源文件或者class文件時,我們有沒有什么辦法直接在jemter中引用?這就是這部分要介紹的內(nèi)容,直接上示例:
1、假如我有一個java 源文件,名為:Myclass.java,代碼如下:
2、Bean Shell使用代碼如下:
在bean shel中通過source("代碼路徑")方法引入java,然后調(diào)用方法和java一樣,new一個class,再調(diào)用里面的add 方法。
3、運行結(jié)果:
五、引用外部class文件:
現(xiàn)在知道如何引用外部文件,有時候如果我們只有class文件怎么辦呢?其實在jmeter中也可以直接引用class文件,示例如下:
1、直接把上例中的java文件編譯成class文件,如何編譯請自行百度。
2、Bean Shell使用代碼如下:
用addClassPath("D:\\")方法引入 class文件,在用import導(dǎo)入包及類,然后就可以像java一樣調(diào)用了
3、運行結(jié)果:
六、引用外部Jar包:
上面四、五介紹了如何引用外部java和class文件,如果文件比較多時我們可以把它們打成一個jar包然后在jemter中調(diào)用,具體如何使用可以看我上一篇有介紹:Jmeter之Bean shell使用(一)。
在這里想補充一點的是jmeter中引入jar的方法:
1、上一篇中已使用過的:把jar包放到j(luò)meter目錄\apache-jmeter-2.13\lib\ext下
2、在Test Plan的右側(cè)面板最下方直接添加需要引用的jar包,如下圖:
七、其它用法:
1、在Test Plan中定義如下三個變量:
2、Bean Shell可腳本如下:
a、bean shell可以接受傳入?yún)?shù),如下圖:${u1} ${u2} ${u3}
b、參數(shù)可以通過bsh.args[]按順序提取
c、bean shell提供了一個內(nèi)置變量Parameters,來保存參數(shù)的集合
3、運行結(jié)果:
下圖中1輸入的這兩句設(shè)置:
ResponseCode = 500;
ResponseMessage = "This is a test";
下圖中2輸入的這兩句設(shè)置:
log.info(Parameters);
log.info(Label);