Technology Sharing

A brief discussion on regular expression user parameters of preprocessor

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

A brief discussion on regular expression user parameters of preprocessor

RegEx User Parameters is a pre-processor that allows users to extract values ​​from the response data based on regular expressions and pass these values ​​as parameters to subsequentHTTP RequestOr anywhere else needed. This feature is highly flexible for handling dynamic content, session IDs, tokens, etc. and is a key component in implementing automated test scripts.

scenes to be used

  1. Dynamic parameter processing: When the target system generates different dynamic parameters (such as session ID, timestamp, etc.) in each request, it is necessary to extract and use these parameters to continue the subsequent requests.
  2. Test data personalization: Personalize the parameters of subsequent requests based on the response content, such as extracting the user ID from the login response for subsequent profile query requests.
  3. Data iteration: If the response contains multiple sets of matching items, you can configure the processor to loop and extract them and apply them to multiple requests, which is suitable for testing list or search result pages.

Configuration steps

  1. Add a regular expression user parameter:
    ○ In the JMeter workbench or test plan, select an HTTP request or other sampler that needs to be parameterized.
    ○ Right-click and select Add > Preprocessor > Regular Expression User Parameter.
  2. Configuration parameters:
    ○ Regular Expression Reference Name: The variable name in the regular expression extractor. The name here must be consistent with that in the regular expression.
    ○ Parameter names regexp group number: the group number of the parameter name, which will be explained in detail in the following examples.
    ○ Parameter values ​​regex group number: the corresponding value of the parameter name group, which will be explained in detail in the following examples.

Example

We use SpringBoot to write the following interface code

 @PostMapping(value = "/login",produces = "application/json;charset=UTF-8")
    public String authenticate(@RequestBody JSONObject request) {

        String validUsername = "admin";
        String validPassword = "password";
        String response = "{"total":2,"data":[{"id":123,"name":"John Doe","email":"[email protected]","phone":"123-456-7890","address":{"street":"123 Main St","city":"New York","state":"NY","zip":"10001"},"interests":["sports","music","travel"]},{"id":456,"name":"Jane Smith","email":"[email protected]","phone":"987-654-3210","address":{"street":"456 Elm St","city":"Los Angeles","state":"CA","zip":"90001"},"interests":["reading","cooking","hiking"]}]}";


        if (request.getString("username").equals(validUsername) && request.getString("password").equals(validPassword)) {
            return response;
        } else {
            return response;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Then we write the following script
insert image description here
Thread Group: Keep default
HTTP request 1: protocol http, server name or IP 127.0.0.1, port number 8091, method POST, path /login, content encoding utf-8, message body data

{
    "username": "admin",
    "password": "password"
    }
  • 1
  • 2
  • 3
  • 4

Regular expression extraction器:设置的参数为引用名称var,正则表达式"id"😦.?),“name”:"(.?)", "email" (as can be seen from the sample code), and our goal is to extract the id and name, the matching number is set to -1, and the default value is NotFound
At this point, we can disable HTTP request 2 and regular expression user parameters, then run the script and view the debug sampler of the result tree. The following results can be seen in the Response Body of the response data:

JMeterVariables:
JMeterThread.last_sample_ok=false
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@4fa1cf1
START.HMS=165132
START.MS=1720515092259
START.YMD=20240709
TESTSTART.MS=1720578466746
_jm__threadgroup__idx=0
_jmeter.U_T
=Thread Group 1-1
var=NotFound
var_1=
var_1_g=2
var_1_g0=“id”:123,“name”:“John Doe”,“email”
var_1_g1=123
var_1_g2=John Doe
var_2=
var_2_g=2
var_2_g0=“id”:456,“name”:“Jane Smith”,“email”
var_2_g1=456
var_2_g2=Jane Smith
var_matchNr=2

From the above content, we can see the value of the var variable extracted by the corresponding regular expression. At this point, we can continue to verify the application of the regular expression user parameter preprocessor and continue to set other component parameters.
HTTP request 2: protocol http, server name or IP 127.0.0.1, port number 8091, method POST, path /login, content encoding utf-8, then add two parameters in the parameters, fill in the names 123 and 456 respectively. Don't worry about the specific meaning for now, I'll keep it a secret here.
Regular expression user parameters: Regular Expression Reference Name is set to var. The parameter name here must be consistent with the parameter name in the regular expression above. Parameter names regexp group number is set to 1, and Parameter values ​​regex group number is set to 2. Why do we set it this way? We can understand that in HTTP request 2, we set 123 and 456. Parameter names regexp group number is set to 1, which can be understood as taking the parameters of var_1_g1 and var_2_g1 from the var parameter. When the parameters filled in our HTTP request 2 are consistent with the parameters here, we correspond to the relationship between parameters and data, paving the way for setting Parameter values ​​regex group number to 2. We can well understand that when Parameter values ​​regex group number is set to 2, the parameters taken become var_1_g2 and var_2_g2.
The next step is to verify our results. Run the script to view the post data of the Request Body in HTTP request 2 in the result tree. Overall, our request failed, but this does not matter. We only verified the content we need to verify, which has nothing to do with the results.

POST http://127.0.0.1:8091/login
POST data:
123=John+Doe&456=Jane+Smith
[no cookies]

From the above POST data, you can see that the corresponding data has been reflected in the request.

Precautions

● Scope: Regular expression user parameters are only effective for the HTTP request or sampler that follows it. If you need to apply it globally, please consider using the "Regular Expression Extractor" in conjunction with variables.
● Debug: Use the View Results Tree listener to check regular expression matches to ensure that data is being extracted correctly.

Summarize

Regular expression user parameters are a powerful feature in JMeter, which simplifies the processing of dynamic data and improves the flexibility and automation of test scripts. Proper configuration and use of this feature can effectively meet the performance testing needs of complex systems.