Interface testing practice, Jmeter regular extraction of response data - detailed arrangement, one article to get through...


Preface

During testing, we often need to process the response data returned by the request. For example, many times the cookie or token or Authorization authorization code will be returned in the Response headers (response headers). At this time, we need to extract it for use by other interfaces.

How to extract the data of Response headers in response data through regular expression in Jmeter?

The general setting interface is as follows:

E1

Send a request and get response data

First, set up an HTTP request as follows:

E2

Next, set up a view result tree, and then execute it to get the response data. The cookie in the picture below is the data we need to extract:

E3

Regularly extract response data

Next, we set up a regular extractor (used to extract cookies) and a debugging post-processor (used to view the extraction results)

E4

Note:
1. Reference name: that is, the variable name, which can be called by the reference name in subsequent requests. 2. Regular expression: fill in the actual situation. 3. Template: {reference name} to call. 2. Regular expression: fill in the actual situation. 3. Template:Reference the name to call 2. Regular expression: fill in the actual situation 3. Template: $ indicates which regular expression needs to obtain the value, 1 represents the first one, -1 represents all, 0 represents random
4. Matching number: 1 represents The first one, -1 represents all, 0 represents random
5. Default value: If the regular expression is not matched, the default value will be used.

Regular description:

.:表示除“\r\n”之外的任意字符
*:表示匹配前面的子表达式任意次
+:表示匹配前面的子表达式1次或多次
?:表示匹配前面的子表达式0次或1.*:贪婪匹配原则,即匹配到不能匹配为止
.*?:非贪婪匹配,即在匹配成功的情况下尽可能少的匹配

Example:
There is a string 7adbcfgfbesw, to match the characters between 7 and b

匹配1:使用 .*7开始之后,遇到第一个b不结束,继续找下一个b,直到不能匹配,即匹配到 adbcfgf

匹配2:使用 .*? ,7开始之后,遇到第一个b就结束了,即匹配到 ad

E5

View extraction resultsExtraction results

E6

The above is the extraction result this time. Here, if it needs to be used in subsequent requests, it can be associated with the call through ${response_data}.

Possible problems

Suppose, we need to extract all the contents in Set-Cookie. At this time, if you still use .*?, you will find that the extraction is unsuccessful, as follows:

E7

E8

E9

It is also very simple to solve this problem. We modify the regular expression and use the .* greedy matching principle.

E10

E11

The above is the content extracted through Jmeter regularity. The method of extracting other data, such as token, Authorization, etc., is similar to the above.

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Struggle is as beautiful as a rainbow, and you need to go through wind and rain to see its beauty. Persistent efforts will light up the way forward and let the flame of dreams burn in your heart. Believe in your own strength and move forward courageously.

Every fighting moment is an opportunity to redefine yourself. No matter how many obstacles lie ahead, as long as you have courage and determination and work hard, you will eventually surpass yourself and shine with your own light.

Every effort is the courage to move forward, and every struggle is the step to pursue your dreams. No matter how many difficulties and obstacles you encounter, persist in perseverance, uphold faith and enthusiasm, and you will use your sweat and wisdom to create a gorgeous and glorious chapter in life!

Guess you like

Origin blog.csdn.net/m0_60054525/article/details/132210756