一、Response Assertion(響應斷言)
二、Size Assertion(數據包字節大小斷言)
三、Duration Assertion(持續時間斷言)
四、beanshell 斷言(自由斷言)
一、Response Assertion(響應斷言)
1. 添加響應斷言
對Web請求的響應結果進行驗證
2. 輸入需要匹配的字符串
此處對於訪問Baidu首頁,需要設置匹配的字符串為“百度一下,你就知道”,表示返回的文本內容若包含有“百度一下,你就知道”,則就算Pass
Response Assertion配置參數
模塊類型 | 選項名稱 | 配置說明 |
---|---|---|
Name | Response Assertion名稱 | |
Comments | 注釋 | |
Apply to | 斷言應用的范圍 | |
Main sample and sub-samples | 作用於父節點取樣器及其子節點取樣器 | |
Main sample only | 僅作用於父節點取樣器 | |
Sub-samples only | 僅作用於子節點取樣器 | |
Jmeter Variable Name to use | 作用於Jmeter變量(輸入框中可輸入Jmeter的變量名稱) | |
Field to Test | 測試的字段 | |
Text Response | 匹配從服務器返回的響應文本(不包括Response Headers) | |
Response Code | 匹配響應狀態碼 | |
Response Message | 匹配響應信息。如:OK | |
Response Headers | 匹配響應頭 | |
Request Headers | 匹配請求頭 | |
URL Sampled | 匹配URL鏈接 | |
Document(text) | 匹配文檔內容 | |
Ignore Status | 一個請求多項響應斷言時,忽略某一項斷言的響應結果,而繼續下一項斷言 | |
Request Data | 匹配請求數據 | |
Pattern Mactching Rules | 匹配的規則 | |
Contains | 返回的結果包括所指定的內容,支持正則匹配 | |
Matches | 根據指定內容進行匹配 | |
Equals | 返回結果與所指定的內容一致 | |
Substring | 返回結果包括所指定結果的字符串,不支持正則匹配 | |
Not | 不進行匹配就算是Pass | |
Or | 暫不確定該模式的用法 | |
Patterns to Test | ||
Patterns to Test | 需要匹配的正則表達式、字符串。可以添加多項,每一項會分開進行驗證,若某一項驗證失敗,則其后的不會再進行驗證。 |
3. 添加:斷言結果(Assertion Results)、查看結果樹(View Results Tree)
4. 運行Test Plan中的線程組,進行斷言檢查
以下可觀察到響應數據中是包含所指定的驗證字符串,Pass
二、Size Assertion(數據包字節大小斷言)
判斷響應結果是否包含正確數量的byte。可定義(=, !=, >, <, >=, <=)
三、Duration Assertion(持續時間斷言)
判斷是否在給定的時間內返回響應結果
四、beanshell 斷言
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的內置變量外,主要通過 Failure 和 FailureMessage來設置斷言結果。
其中腳本內容如下:
if ("200".equals(""+ResponseCode) == false ) { // 響應碼不等於200時,設置斷言失敗,並輸出失敗信息 Failure=true ; FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ; print ( "the return code is " + ResponseCode); // this goes to stdout log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file } else { // 響應碼等於200時,設置斷言成功,並輸出成功信息 Failure=false; FailureMessage = "Return true, and the response code was " + ResponseCode; } }