1、什麼是Bean Shellhtml
官網:http://www.BeanShell.org/java
2、Jmeter有哪些Bean Shellshell
定時器: BeanShell Timerapache
前置處理器:BeanShell PreProcessorapi
採樣器: BeanShell Samplereclipse
後置處理器:BeanShell PostProcessor函數
斷言: BeanShell斷言工具
監聽器: BeanShell Listener測試
3、BeanShell的用法加密
在此介紹下BeanShell PreProcessor的用法,其它的beahshell能夠類推。在此咱們使用beahshell調用本身寫的工具類,工具類實現了密碼的加、解密功能:
一、在eclipse寫好代碼,而後把該類打成jar包(在類上點擊右鍵->Export->jar file)
二、把jar包放到jmeter目錄\apache-jmeter-2.13\lib\ext下
三、打開jmeter,添加一個http sampler(調用登陸接口),在sampler下添加一個BeanShell PreProcessor
四、在beanshell PreProcessor中導入咱們的jar包,調用裏面的加、解密碼方法,把結果保存在jmeter變量中,下面兩個方法是beanshell中咱們最經常使用到的:
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);
五、把加密後的密碼存到jmeter變量中,而後在http sampler中就能夠經過${encode}進行使用了:
六、執行腳本:
4、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
1、操做變量
2、操做屬性
3、自定義函數
4、引用外部java文件
5、引用外部class文件
6、引用外部Jar包
7、其它用法(接受參數, log等)
1、操做變量:經過使用Bean shell內置對象vars能夠對變量進行存取操做
a) vars.get("name"):從jmeter中得到變量值
b) vars.put("key","value"):數據存到jmeter變量中
2、操做屬性:經過使用Bean shell內置對象props 能夠對屬性進行存取操做
a) props.get("START.HMS"); 注:START.HMS爲屬性名,在文件jmeter.properties中定義
b) props.put("PROP1","1234");
3、自定義函數:
在BeanShell中,咱們可使用java語言自定義函數來處理特定的邏輯,結合BeanShell的內置對象進行變量的存取,方便咱們進行測試提升腳本的靈活性。
示例:
一、在Test Plan中添加一個變量:hello = kitty
二、Debug sampler-1和Debug sampler-2什麼都不處理,用來查詢對比beahshell處理先後的結果
三、BeanShell Sampler中的腳本以下:
四、運行結果:
4、引用外部java文件:
有沒有以爲上面(三)中自定義函數這樣的方式太麻煩而且也不美觀?並且若是咱們已經有現成的java源文件或者class文件時,咱們有沒有什麼辦法直接在jemter中引用?這就是這部分要介紹的內容,直接上示例:
一、假如我有一個java 源文件,名爲:Myclass.java,代碼以下:
package test; public class Myclass { public int add(int a, int b) { return a + b; } }
二、Bean Shell使用代碼以下:
在bean shel中經過source("代碼路徑")方法引入java,而後調用方法和java同樣,new一個class,再調用裏面的add 方法。
三、運行結果:
5、引用外部class文件:
如今知道如何引用外部文件,有時候若是咱們只有class文件怎麼辦呢?其實在jmeter中也能夠直接引用class文件,示例以下:
一、直接把上例中的java文件編譯成class文件,如何編譯請自行百度。
二、Bean Shell使用代碼以下:
用addClassPath("D:\\")方法引入 class文件,在用import導入包及類,而後就能夠像java同樣調用了
三、運行結果:
6、引用外部Jar包:
上面4、五介紹瞭如何引用外部java和class文件,若是文件比較多時咱們能夠把它們打成一個jar包而後在jemter中調用,具體如何使用能夠看我上一篇有介紹:Jmeter之Bean shell使用(一)。
在這裏想補充一點的是jmeter中引入jar的方法:
一、上一篇中已使用過的:把jar包放到jmeter目錄\apache-jmeter-2.13\lib\ext下
二、在Test Plan的右側面板最下方直接添加須要引用的jar包,以下圖:
7、其它用法:
一、在Test Plan中定義以下三個變量:
二、Bean Shell可腳本以下:
a、bean shell能夠接受傳入參數,以下圖:${u1} ${u2} ${u3}
b、參數能夠經過bsh.args[]按順序提取
c、bean shell提供了一個內置變量Parameters,來保存參數的集合
三、運行結果:
下圖中1輸入的這兩句設置:
ResponseCode = 500;
ResponseMessage = "This is a test";
下圖中2輸入的這兩句設置:
log.info(Parameters);
log.info(Label);