Basic usage of JMeter regular expression extractor and JSON extractor, novices must know it!

Recently, I have been using JMeter for automated interface testing, and I have used regular expression extractors and JSON extractors quite a lot. I thought I would share this with you, hoping to inspire everyone’s interface automated testing projects.

In JMeter, both regular expressions and JSON extractors are used to extract the required content from response data, but their usage scenarios are slightly different. The regular expression extractor is suitable for processing unstructured or irregularly formatted response data, while the JSON extractor is suitable for processing structured JSON response data.

Regular Expression Extractor: Regular expression extractor is a built-in feature of JMeter that is used to extract text of a specific pattern from response data. It is suitable for those situations where the content in the response data does not have a well-defined structure.

For example, when the response data is free-form text, HTML, XML, or other unstructured data, a regular expression extractor can be used to capture the information, and an appropriate regular expression needs to be written to match and capture what is required.

JSON Extractor: JSON Extractor is a plug-in for JMeter that is specially used to extract data from JSON formatted response data. It is optimized for JSON data structure and provides a more convenient way to parse and extract JSON data.

You can use the JSON extractor to locate and extract values ​​from a JSON object, array, or property based on a JSON path expression. The JSON extractor is more suitable for processing structured response data, such as JSON format data returned by API responses.

1. Usage scenarios

The JSON data returned by the A interface is as follows. The B interface needs to use the return parameter id_card_num of the A interface, so the parameters returned by the A interface need to be extracted. There is a variable such as card_num_test, which is directly in the form of ${card_num_test} in the B interface. Just call it.

picture

2. Regular expression extractor

1. Create a new http request

picture

2. Add regular expression extractor

Select the http request, right-click – Add – Post-Processor – Regular Expression Extractor

picture

3. Fill in the parameters of the regular expression

picture

Name: Custom

Apply to :

Main sample and sub-samples: The matching range includes the current parent sampler and covers the sub-sampler

Main sample only: Default, the matching scope is the current parent sampler (in most cases, select the default)
Sub-samples only: Only match sub-samplers
JMeter Variable Name to use: Supports matching of JMeter variable values ​​(in the input box Variable names that can be entered into jmeter)
Response fields to check:

Body: The main part of the response data
Body (unescaped): For the replaced response code part
Body as a Document: The returned content is matched as a document
Information header
Request Headers: The request header part
URL: URL link
Response code: Response code, for example HTTP return code 200 indicates success.
Response information: response information, such as the word "success" or "OK" returned if the processing is successful.
Reference name: the name of the parameter to be referenced in the next request. If card_num_test is filled in, you can use $card_num_test} to reference it.

Regular expression: Write a regular expression to match the content to be extracted, such as "id_card_num": "(.+?)" (
): The enclosed part is what is to be extracted.
.: Matches any string.
+: one or more times.
?: Don't be too greedy, stop after finding the first match.

Template: Specify how to process the matching results. For example, − 1 -1−1 means taking all values, 0 00 means taking a random value, 1 11 means taking the first value, and 2 22 means taking the second value.

Matching numbers: 1 represents all values, 0 represents random values

Default value: If the regular expression cannot obtain a value, this default value is used

4. Add a Debug Sampler

Can be used for debugging and verifying whether the regular expression is correct.

picture

picture

5. Execute the script and view the results

It is consistent with the value returned by the interface, which proves that the regular expression is written correctly. When the B interface needs to use this value, it is directly called in the form of ${card_num_test}.

picture

3. JSON extractor

1. Create a new http request

picture

2. Add regular expression extractor

Select the http request, right-click – Add – Post-processor – JSON Extractor

picture

3. Fill in the parameters of the JSON extractor

picture

Name: Custom

Apply to: Application scope, generally defaults to Main sample only, which can be selected according to actual usage needs

Main sample and sub-samples: Acts on main requests and sub-requests

Main sample only: only applies to the main request

Sub-samples only: only applies to sub-requests

JMeter Variable: Acts on JMeter variables (the variable name of JMeter can be entered in the input box); usage can be used to extract the value in the regular expression extractor

Names of Created variables: As referenced variable names, multiple variable names are separated by semicolons; customized

JSON Path expessions: The format is . (level name), this example is . (level name), this example is . (level name), this example is .data.id_card_num

Match No.: Which value to take, 0 means random, -1 means taking all array data, 1 means taking a variable value, which is related to the set variable name. In this example, fill in -1 (Note: If you fill in 1, the following When referencing a variable, just use cardnumtest. If you fill in - 1, an array will be returned. When you reference the variable later, use {card_num_test}. If you fill in -1, an array will be returned. When you reference the variable later, Just use card 
n  ​um 
test ​est  . If you fill in −1, an array will be returned. When referencing variables later, use the form {card_num_test_1}, ${card_num_test_2})



Compute concatenation var(suffix_ALL): All matched values ​​are saved and the default value is empty.

Default Value: The content displayed when the value fails. The default value is empty.

4. Add a Debug Sampler

Can be used for debugging and verifying whether the regular expression is correct.

picture

picture

5. Execute the script and view the results

It is consistent with the value returned by the interface, which proves that the regular expression is written correctly. When the B interface needs to use this value, it is directly called in the form of ${card_num_test_1}.

picture

That’s it for today’s sharing~

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!   

Guess you like

Origin blog.csdn.net/2301_78843735/article/details/133081071