Jmeter(十二)关联

关联在实际业务需求中是随处可见的,比如:支付需要提交订单成功的订单号;修改个人资料需要登录成功响应报文信息。。。总之关联无处不在,今天来记一记Jmeter的关联功能。

  Jmeter关联的方法比较常用的是正则表达式提取器,正则表达式提取器属于后置处理器,那么久抛出了一个比较大的知识点----正则表达式;

  其实,正则表达式就是一种文本模式,相信都在windows我的电脑中搜索过文件嘛,那么肯定使用过“*”,其实都是类似。

  记几个比较常用的:

^ ----->为匹配输入字符串的开始位置。

      $ ----->为匹配输入字符串的结束位置。

. ------>匹配单字符。

      + ------>匹配一次或多次(大于等于1次)

      ?------>贪婪符,匹配到立即停止。

      \d------->匹配一个数字字符

      \n ------>匹配一个换行符

      \r ------->匹配一个回车符

      。。。。。

 官方文档:

AttributeDescriptionRequired

NameDescriptive 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 checkThe 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 NameThe 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 ExpressionThe 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

TemplateThe 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 ValueIf 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 valueIf 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    }  }

XPathJSONPath结果

/store/book/author$.store.book[*].author书点所有书的作者

//author$..author所有的作者

/store/*$.store.*store的所有元素。所有的bookst和bicycle

/store//price$.store..pricestore里面所有东西的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开始数。

XPathJSONPathDescription

/$表示根元素

.@ 当前元素

/. or []子元素

..n/a父元素

//..递归下降,JSONPath是从E4X借鉴的。

**通配符,表示所有的元素

@n/a 属性访问字符

[][]子元素操作符

|[,]连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。

n/a[start:end:step]数组分割操作从ES4借鉴。

[]?()应用过滤表示式

n/a()脚本表达式,使用在脚本引擎下面。

()n/aXpath分组







欢迎任何形式的转载,但请务必注明出处。 限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。 ---紫陌花间客

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容