Jmeter(十二)關聯

  關聯在實際業務需求中是隨處可見的,好比:支付須要提交訂單成功的訂單號;修改我的資料須要登陸成功響應報文信息。。。總之關聯無處不在,今天來記一記Jmeter的關聯功能。html

  Jmeter關聯的方法比較經常使用的是正則表達式提取器,正則表達式提取器屬於後置處理器,那麼久拋出了一個比較大的知識點----正則表達式;web

  其實,正則表達式就是一種文本模式,相信都在windows個人電腦中搜索過文件嘛,那麼確定使用過「*」,其實都是相似。正則表達式

  記幾個比較經常使用的:express

      ^ ----->爲匹配輸入字符串的開始位置。json

      $ ----->爲匹配輸入字符串的結束位置。windows

      . ------>匹配單字符。
數組

      + ------>匹配一次或屢次(大於等於1次)restful

      ?------>貪婪符,匹配到當即中止。app

      \d------->匹配一個數字字符less

      \n ------>匹配一個換行符

      \r ------->匹配一個回車符

      。。。。。

  

 

 官方文檔:

Attribute Description Required
Name Descriptive name for this element that is shown in the tree. No
Apply to: This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
  • Main sample only - only applies to the main sample
  • Sub-samples only - only applies to the sub-samples
  • Main sample and sub-samples - applies to both.
  • JMeter Variable - assertion is to be applied to the contents of the named variable
Matching is applied to all qualifying samples in turn. For example if there is a main sample and 3 sub-samples, each of which contains a single match for the regex, (i.e. 4 matches in total). For match number = 3, Sub-samples only, the extractor will match the 3rd sub-sample. For match number = 3, Main sample and sub-samples, the extractor will match the 2nd sub-sample (1st match is main sample). For match number = 0 or negative, all qualifying samples will be processed. For match number > 0, matching will stop as soon as enough matches have been found.
Yes
Field to check The following fields can be checked:
  • Body - the body of the response, e.g. the content of a web-page (excluding headers)
  • Body (unescaped) - the body of the response, with all Html escape codes replaced. Note that Html escapes are processed without regard to context, so some incorrect substitutions may be made.

     

    Note that this option highly impacts performances, so use it only when absolutely necessary and be aware of its impacts
  • Body as a Document - the extract text from various type of documents via Apache Tika (see View Results Tree Document view section).

     

    Note that the Body as a Document option can impact performances, so ensure it is OK for your test
  • Request Headers - may not be present for non-HTTP samples
  • Response Headers - may not be present for non-HTTP samples
  • URL
  • Response Code - e.g. 200
  • Response Message - e.g. OK
Headers can be useful for HTTP samples; it may not be present for other sample types.
Yes
Reference Name The name of the JMeter variable in which to store the result. Also note that each group is stored as [refname]_g#, where [refname] is the string you entered as the reference name, and # is the group number, where group 0 is the entire match, group 1 is the match from the first set of parentheses, etc. Yes
Regular Expression The regular expression used to parse the response data. This must contain at least one set of parentheses "()" to capture a portion of the string, unless using the group $0$. Do not enclose the expression in / / - unless of course you want to match these characters as well. Yes
Template The template used to create a string from the matches found. This is an arbitrary string with special elements to refer to groups within the regular expression. The syntax to refer to a group is: '$1$' to refer to group 1, '$2$' to refer to group 2, etc. $0$ refers to whatever the entire expression matches. Yes
Match No. (0 for Random) Indicates which match to use. The regular expression may match multiple times.
  • Use a value of zero to indicate JMeter should choose a match at random.
  • A positive number N means to select the nth match.
  • Negative numbers are used in conjunction with the ForEach Controller - see below.
Yes
Default Value If the regular expression does not match, then the reference variable will be set to the default value. This is particularly useful for debugging tests. If no default is provided, then it is difficult to tell whether the regular expression did not match, or the RE element was not processed or maybe the wrong variable is being used.

However, if you have several test elements that set the same variable, you may wish to leave the variable unchanged if the expression does not match. In this case, remove the default value once debugging is complete.

No, but recommended
Use empty default value If the checkbox is checked and Default Value is empty, then JMeter will set the variable to empty string instead of not setting it. Thus when you will for example use ${var} (if Reference Name is var) in your Test Plan, if the extracted value is not found then ${var} will be equal to empty string instead of containing ${var} which may be useful if extracted value is optional. No

 

 正則表達式會寫,用這個很eazy。

  現現在,restful風格(http+json)的接口非常流行,響應信息爲json格式的,那麼就還能簡單一點,不用正則表達式那麼複雜。

  而json的數據類型有對象、數組、字符串、數字(整型、浮點)、布爾、null;使用jsonpath語法來進行提取判斷。

  Jmeter也有專門提取json的提取器,固然是第三方插件咯。。。Json Path Extractor

  

  json是key-value類型的,固然也會碰到數組,關於這些也來記一記。

  (參考:http://goessner.net/articles/JsonPath/)

Demo(一段json報文):

{ "store": {
    "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }
XPath JSONPath 結果
/store/book/author $.store.book[*].author
書點全部書的做者
//author $..author
全部的做者
/store/* $.store.*
store的全部元素。全部的bookst和bicycle
/store//price $.store..price
store裏面全部東西的price
//book[3] $..book[2]
第三個書
//book[last()] $..book[(@.length-1)] 最後一本書
//book[position()<3] $..book[0,1]
$..book[:2]
前面的兩本書。
//book[isbn] $..book[?(@.isbn)]  過濾出全部的包含isbn的書。
//book[price<10] $..book[?(@.price<10)] 過濾出價格低於10的書。
//* $..*
全部元素。

能夠看出其中的缺省符,通配符仍是很經常使用的。常常會懵的就是碰到數組;還有就是jsonpath是從0開始數節點。

 

  那麼有jsonpath,也就有xpath^_^

  

  一樣,它對於xml類型的報文信息提取比較簡潔,數節點便可^_^。上方表格第一列即是xpath的相關語法。只是須要謹記的一點就是jsonpath數節點是從0開始數,而xpath數節點是從1開始數。

  

XPath JSONPath Description
/ $ 表示根元素
. @  當前元素
/ . or [] 子元素
.. n/a 父元素
// .. 遞歸降低,JSONPath是從E4X借鑑的。
* * 通配符,表示全部的元素
@ n/a  屬性訪問字符
[] []
子元素操做符
| [,]
鏈接操做符在XPath 結果合併其它結點集合。JSONP容許name或者數組索引。
n/a [start:end:step]
數組分割操做從ES4借鑑。
[] ?()
應用過濾表示式
n/a ()
腳本表達式,使用在腳本引擎下面。
() n/a Xpath分組
相關文章
相關標籤/搜索