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

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

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

  记几个比较常用的:

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

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

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

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

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

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

      \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分组

   

Jmeter(十二)关联的更多相关文章

  1. JMeter(十)-正则表达式关联

    jmeter中,接口自动化的关键在于参数关联.比如需要登录的接口,如何调用登录口令?一个增删改查的闭环,如何将接口参数上下传递?下面就以实际的例子来仔细说一说 1:登录接口 这里有一个实际的登录接口, ...

  2. Jmeter(十二)_打印时间戳

    Jmeter中提供了一种函数,可以打印时间戳,如下图 年: yyyy 月:MM 日:dd 时: HH 分: mm 秒:ss 关于时间戳的格式,可以自由组合定义,这里我写成这样 yyyy-MM-dd H ...

  3. JMeter 十二:命令行执行

    参考文档:http://jmeter.apache.org/usermanual/get-started.html#non_gui 真正开始跑压力测试时,我们就不能使用GUI模式了.这时候需要采用命令 ...

  4. Jmeter(十二)响应断言之响应文本和响应信息的差别

    在Jmeter的后置处理器中添加响应断言, 要测试的响应字段中有两个很难区分的选项, 响应文本和响应信息. 我做了两个小实验来进行区别. 1. 用Fiddler捕捉了一个POST请求, 其响应是suc ...

  5. Jmeter(十二) - 从入门到精通 - JMeter逻辑控制器 - 终篇(详解教程)

    1.简介 Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in which Samplers are processed.”. 意思 ...

  6. JMeter—监听器(十二)

    参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第七节监听器用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默 ...

  7. Jmeter学习(三十二)调试工具Debug Sampler(转载)

    转载自 http://www.cnblogs.com/yangxia-test 一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug S ...

  8. CRL快速开发框架系列教程十二(MongoDB支持)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  9. 我的MYSQL学习心得(十二) 触发器

    我的MYSQL学习心得(十二) 触发器 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数 ...

  10. <构建之法>第十一章、十二章有感

    十一章:软件设计与实现 工作时要懂得平衡进度和质量.我一直有一个困扰:像我们团队这次做 男神女神配 社区交友网,我负责主页的设计及内容模块,有个队友负责网站的注册和登录模块,有个队友负责搜索模块,有个 ...

随机推荐

  1. 对中文进行MD5加密的注意事项(Java版,编码问题)

    http://blog.csdn.net/tongdao/article/details/20690187 在工作中需要和第三方进行Http通信,在通信内容中有几个参数涉及到了中文.自己在进行MD5加 ...

  2. Spring boot Mybatis 整合

    PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版)   这篇博客里用到了怎样 生成 mybatis 插件来写程 ...

  3. dc-vastinspector

    https://developers.google.com/interactive-media-ads/docs/sdks/html5/vastinspector hosts: https://gis ...

  4. mysql自增主键

    MariaDB [test]> create table test1(id int primary key auto_increment,name varchar(20))auto_increm ...

  5. pushpin Server-sent events && openresty 集成试用

    前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能 备注: 环境依然使用的是docker-compose运行 环境准备 docker-compose 文件 ver ...

  6. streamdataio 实时数据分发平台

    streamdataio 是一个实时的数据分发平台(当然是收费的,但是设计部分可以借鉴),我们可以通过这个平台 方便的拉取rest api 数据,或者发布数据到后端,streamdataio 可以帮助 ...

  7. Babelfish 开源通用代码解析服务

    Babelfish 是一个开源的代码解析服务 参考架构 支持的语言 bash go java javascript php ruby c++ typescript 功能 我们可以使用此工具,进行大规模 ...

  8. lamp安装总结

    1.安装准备   建一个目录用于存放各软件包的压缩文件, 如我把我的源码文件都放在了 /software目录下 切换到/software目录下,执行 wget http://dev.mysql.com ...

  9. [转]Java对象的序列化和反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  10. python生成随机数、随机字符串

    python生成随机数.随机字符串 import randomimport string # 随机整数:print random.randint(1,50) # 随机选取0到100间的偶数:print ...