[P15] JMeter Regular Expression Extractor (Regular Expression Extractor)


1. Regular Expression Extractor (Regular Expression Extractor) parameter description

接口需要关联时,可以通过正则表达式提取所需要的值

Right click >>> Add >>> Post Processor >>> Regular Expression Extractor

insert image description here

Apply to

  • Main sample and sub-samples: Matches include the current parent sampler and overrides the sub-samplers

  • Main sample only: default; the matching range is the current parent sampler

  • Sub-samples only : match only sub-samplers

  • JMeter Variable Name to use: Supports matching of Jemter variable values ​​(you can enter the variable name of jmeter in the input box)

Response fields to check (Filed to check)

  • Body: The body part of the response data

  • Body (unescaped): for the replaced response code part

  • Body as a Document: returns the content as a document for matching

  • Response Headers: Response header part

  • Request Headers: request header part

  • URL: URL link

  • Response Code: Response code. For example, the HTTP return code 200 means success

  • Response Message (Resopnse Message): Response message. For example, if the processing is successful, the word "success" or "OK" will be returned

(1) Reference name (Name of created variable): Request the name of the variable to be referenced, such as filling in result_num

(2), Regular Expression (Regular Expression): the regular expression corresponding to the business

"id":"(.*?)","name":(.\*?)"

(3) Template (Template): the group number corresponding to the regular expression, starting from 1; for example: $1$ means to take the value of id, $2$ means to take the value of name, which is the number corresponding to the expression brackets

(4) Matching numbers (0 for random) (Match No. (0 for Random): 0 for random values, n for the number of matching values, -1 for all; for example: take 1 for the first in the message 1 object matches, 2 means the second object in the message matches

(5), Default Value (Default Value): If the parameter does not get a value, then give a value by default to let it take

Regular Expressions - Tutorial | Novice

2. Preparation

Mumu Fresh: http://111.231.103.117/#/login

  • After entering the webpage, log in, and the page provides the account and password

  • Type "shrimp" in the search box

  • Right-click to check or press F12 to open the debugging tool and click Search

insert image description here

3. Test plan design

(1), right click on the test plan <<< add <<< configuration element <<< HTTP request default value

insert image description here

  • protocol: http

  • Server name or IP: 111.231.103.117

  • Port: 8083

  • Content-Encoding: utf-8

(2) On the web page, click on the network during debugging, find the request, click on the header to find the request header, and copy all the request headers

insert image description here

(3), right click on the test plan <<< add <<< configuration element <<< HTTP header manager

insert image description here

  • Click Add from clipboard, the copied value will be added automatically

(4), right click on the test plan <<< add <<< thread (user) <<< thread group

(5) On the web page, click on the network during debugging, find the request, click on the load, and copy the parameter value

insert image description here

(6), right click on the thread group <<< add <<< sampler <<< HTTP request

insert image description here

  • Protocol: GET

  • Path: /product/list

  • Click Add from Clipboard, it will be added automatically; you can also add it manually

(7), HTTP request right click <<< add <<< post processor <<< regular expression extractor

insert image description here

  • 勾选 Main sample and sub-samples

匹配范围包括当前父采样器并覆盖子采样器

  • Reference name: userParam

变量名称:userParam

  • Regular expression:"id":(.*?),"name":"(.*?)

(.*?):表示匹配任意字符

HTTP请求返回的响应数据响应体中的返回数据格式:[{"id":39,"name":"即食大虾","image":"http://111.231.103.117:8083/images/new-xia.png","detail":"干虾烤虾大对虾干500g淡干烤海虾干货海鲜干货年货","categoryId":13,"price":40,"stock":21925,"status":1,"createTime":"2020-06-28 16:06:34","updateTime":"2020-02-11 00:51:59"},{"id":27,"name":"波士顿大龙虾鲜活冰鲜海鲜水产大只装波龙澳洲龙虾 400-500g 一只装","image":"http://111.231.103.117:8083/images/new-longxia.png","detail":"类别:澳洲龙虾特产品类:澳洲龙虾国产/进口:国产保存状态:冰鲜原产地:中国大陆","categoryId":13,"price":222,"stock":22123,"status":1,"createTime":"2020-06-28 16:06:34","updateTime":"2020-02-11 00:11:30"}]

  • Template: $2$

取第二个参数

  • Matching numbers (0 for random): 2

取第二组数据;第二组数据中的id和name

(8), right click on thread group <<< add <<< listener <<< view result tree

(9), right click on the thread group <<< add <<< sampler <<< debug sampler

(10), click to start, click to view the result tree, view the request and sampler information

insert image description here

HTTP请求中的响应数据,响应体中的数据可以看到,一共有两个符合查询规则的数据

(11) Click the debug sampler to view the response body under the response data

insert image description here

userParam:正则表达式提取的参数名,获取到的参数值

userParam_g:第2组

userParam_g0:第二组的数据,id和name

userParam_g1:第二组数据的第一个参数,id

userParam_g2:第二组数据的第一个参数,id

(12), Right click on the thread group <<< Add <<< Sampler <<< JSR223 Sampler, write scripts, open the log, clear all, click start

点击选项菜单,点击日志查看

insert image description here

log.info("输出变量:${userParam}")
log.info("输出变量:"+vars.get("userParam"));

如图:可以使用变量名获取参数值

Guess you like

Origin blog.csdn.net/qq_45138120/article/details/130589691