正则表达式
参考官网
https://jmeter.apache.org/usermanual/regular_expressions.html
主要使用 (.+?)
来进行匹配。
比如服务器返回结果中
{"success":true,"topic_id":"615e617af7759804f43dca48"}
从结果中提取数据 615e617af7759804f43dca48
可以使用正则表达式
"topic_id":"(.+?)"
找到数据的特征,将要提取的部分使用 (.+?)
来替代。
当服务器返回结果比较复杂的时候。
比如
{"success":true,"data":{"has_read_messages":[{"id":"615e6129f7759804f43dc9df","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e6129f7759804f43dc9dc","title":"helloworld","last_reply_at":"2021-10-07T02:53:29.557Z"},"reply":{"id":"615e6129f7759804f43dc9de","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:53:29.531Z"},"create_at":"2021-10-07T02:53:29.535Z"},{"id":"615e60fef7759804f43dc9a8","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e60fdf7759804f43dc9a5","title":"helloworld","last_reply_at":"2021-10-07T02:52:46.443Z"},"reply":{"id":"615e60fef7759804f43dc9a7","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:52:46.405Z"},"create_at":"2021-10-07T02:52:46.420Z"},{"id":"615e6010f7759804f43dc93a","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e6010f7759804f43dc935","title":"helloworld","last_reply_at":"2021-10-07T02:48:48.536Z"},"reply":{"id":"615e6010f7759804f43dc939","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:48:48.513Z"},"create_at":"2021-10-07T02:48:48.516Z"},{"id":"615e5faaf7759804f43dc8e2","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5faaf7759804f43dc8df","title":"helloworld","last_reply_at":"2021-10-07T02:47:06.519Z"},"reply":{"id":"615e5faaf7759804f43dc8e1","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:47:06.500Z"},"create_at":"2021-10-07T02:47:06.503Z"},{"id":"615e5f93f7759804f43dc8c8","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5f93f7759804f43dc8c5","title":"helloworld","last_reply_at":"2021-10-07T02:46:43.503Z"},"reply":{"id":"615e5f93f7759804f43dc8c7","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:46:43.480Z"},"create_at":"2021-10-07T02:46:43.488Z"},{"id":"615e5f78f7759804f43dc8bb","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5f78f7759804f43dc8b8","title":"helloworld","last_reply_at":"2021-10-07T02:46:16.514Z"},"reply":{"id":"615e5f78f7759804f43dc8ba","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:46:16.491Z"},"create_at":"2021-10-07T02:46:16.495Z"},{"id":"615e5e86f7759804f43dc7fa","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5e86f7759804f43dc7f4","title":"helloworld","last_reply_at":"2021-10-07T02:42:15.361Z"},"reply":{"id":"615e5e86f7759804f43dc7f8","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":[],"create_at":"2021-10-07T02:42:14.555Z"},"create_at":"2021-10-07T02:42:14.571Z"},{"id":"615e5e73f7759804f43dc7ce","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5e73f7759804f43dc7c8","title":"helloworld","last_reply_at":"2021-10-07T02:41:55.574Z"},"reply":{"id":"615e5e73f7759804f43dc7cd","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:41:55.545Z"},"create_at":"2021-10-07T02:41:55.548Z"},{"id":"615e5d93f7759804f43dc71a","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5d93f7759804f43dc717","title":"helloworld","last_reply_at":"2021-10-07T02:38:11.511Z"},"reply":{"id":"615e5d93f7759804f43dc719","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":[],"create_at":"2021-10-07T02:38:11.488Z"},"create_at":"2021-10-07T02:38:11.492Z"},{"id":"615e5cc3f7759804f43dc6d3","type":"reply","has_read":true,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e5cc3f7759804f43dc6d0","title":"helloworld","last_reply_at":"2021-10-07T02:34:43.502Z"},"reply":{"id":"615e5cc3f7759804f43dc6d2","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":[],"create_at":"2021-10-07T02:34:43.498Z"},"create_at":"2021-10-07T02:34:43.507Z"},{"id":"615e583ff7759804f43dc46e","type":"reply","has_read":true,"author":{"loginname":"zhanglanling","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d663ff7759804f43d9615","title":"黑恶黑hi很好的","last_reply_at":"2021-10-07T02:15:27.523Z"},"reply":{"id":"615e583ff7759804f43dc46d","content":"<div class=\"markdown-text\"><p>halouhalouhalouzaima</p>\n</div>","ups":[],"create_at":"2021-10-07T02:15:27.520Z"},"create_at":"2021-10-07T02:15:27.524Z"},{"id":"615e583bf7759804f43dc468","type":"reply","has_read":true,"author":{"loginname":"zhanglanling","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d663ff7759804f43d9615","title":"黑恶黑hi很好的","last_reply_at":"2021-10-07T02:15:27.523Z"},"reply":{"id":"615e583bf7759804f43dc467","content":"<div class=\"markdown-text\"><p>halouhalouhalouzaima</p>\n</div>","ups":[],"create_at":"2021-10-07T02:15:23.494Z"},"create_at":"2021-10-07T02:15:23.498Z"},{"id":"615e5773f7759804f43dc404","type":"reply","has_read":true,"author":{"loginname":"zhanglanling","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d663ff7759804f43d9615","title":"黑恶黑hi很好的","last_reply_at":"2021-10-07T02:15:27.523Z"},"reply":{"id":"615e5773f7759804f43dc402","content":"<div class=\"markdown-text\"><p>halouhalouhalouzaima</p>\n</div>","ups":[],"create_at":"2021-10-07T02:12:03.447Z"},"create_at":"2021-10-07T02:12:03.453Z"},{"id":"615d7d98f7759804f43dbc19","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7d98f7759804f43dbc16","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:42:32.559Z"},"reply":{"id":"615d7d98f7759804f43dbc18","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:42:32.556Z"},"create_at":"2021-10-06T10:42:32.560Z"},{"id":"615d7d52f7759804f43dbc0d","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7d52f7759804f43dbc0a","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:41:22.602Z"},"reply":{"id":"615d7d52f7759804f43dbc0c","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:41:22.599Z"},"create_at":"2021-10-06T10:41:22.603Z"},{"id":"615d7d41f7759804f43dbbfb","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7d40f7759804f43dbbf8","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:41:05.353Z"},"reply":{"id":"615d7d40f7759804f43dbbfa","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:41:04.642Z"},"create_at":"2021-10-06T10:41:05.362Z"},{"id":"615d7d3bf7759804f43dbbf3","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7d3bf7759804f43dbbf0","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:40:59.498Z"},"reply":{"id":"615d7d3bf7759804f43dbbf2","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:40:59.494Z"},"create_at":"2021-10-06T10:40:59.499Z"},{"id":"615d7cfbf7759804f43dbbb6","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7cfaf7759804f43dbbb2","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:39:55.435Z"},"reply":{"id":"615d7cfbf7759804f43dbbb5","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:39:55.434Z"},"create_at":"2021-10-06T10:39:55.437Z"},{"id":"615d7c74f7759804f43dbb4a","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7c74f7759804f43dbb47","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:37:40.521Z"},"reply":{"id":"615d7c74f7759804f43dbb49","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:37:40.519Z"},"create_at":"2021-10-06T10:37:40.523Z"},{"id":"615d7c3bf7759804f43dbb18","type":"reply","has_read":true,"author":{"loginname":"zyjzyj","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615d7c3bf7759804f43dbb15","title":"小祁小祁小祁","last_reply_at":"2021-10-06T10:36:43.565Z"},"reply":{"id":"615d7c3bf7759804f43dbb17","content":"<div class=\"markdown-text\"><p>qiqiqiqiqiqiqiqiqiqiqi</p>\n</div>","ups":[],"create_at":"2021-10-06T10:36:43.556Z"},"create_at":"2021-10-06T10:36:43.566Z"}],"hasnot_read_messages":[{"id":"615e617af7759804f43dca4b","type":"reply","has_read":false,"author":{"loginname":"wangdongdong","avatar_url":"/public/images/userlogo.png"},"topic":{"id":"615e617af7759804f43dca48","title":"helloworld","last_reply_at":"2021-10-07T02:54:50.564Z"},"reply":{"id":"615e617af7759804f43dca4a","content":"<div class=\"markdown-text\"><p>你好,虽然你说的很对!</p>\n</div>","ups":["6153dd8df7759804f43d3fd2"],"create_at":"2021-10-07T02:54:50.536Z"},"create_at":"2021-10-07T02:54:50.540Z"}]}}
上面的数据特别多。
从上面提取 未读消息 的ID。
经过分析发现
我们要寻找的字段 前面有 "hasnot_read_messages":[{"id":"
的特征。
所以使用正则表达式
复制特征部分
"hasnot_read_messages":[{"id":"615e617af7759804f43dca4b"
将要取的值改为
(.+?)
"hasnot_read_messages":[{"id":"(.+?)"
但是,需要注意,
[
{
在正则中也有特殊的含义。所以上面的正则会报错。所以,在正则中。 如果
[{
作为普通字符来用。需要在前面添加\
, 所以上面的正则表达式"hasnot_read_messages":\[\{"id":"(.+?)"
JMeter中的使用
在
获取未读消息和已读消息
接口上添加 正则表达式提取器
【右键】—【添加】—【后置处理器】—【正则表达式提取器】
添加完成之后,根据上面的业务,提取未读消息id
在下游接口中引用变量。
可以看到,不管是 json提取器,正则表达式提取器,都可以提取到数据。
总结
- 怎么处理接口中的上下游传参?
- 使用设置变量的方式,在JMeter中可以通过使用 json提取器和正则表达式提取器来设置变量
- 下游接口中调用变量即可。
- 正则表达式和json提取器的区别?
- 他们都是为了解决同样的问题。
- 当服务器返回结果字段比较少的时候,我习惯直接使用json提取器提取变量。
- 当服务器返回的字段比较多,也可以使用json 一层一层去获取即可。
- 也可以使用正则表达式直接提取结果。
- 当数据比较复杂的时候,也可以使用JMESPath 提取器。
- 所以这两个提取器我都比较熟悉。都可以用。
- 他们都是为了解决同样的问题。