1 Introduction
The previous article introduced how to change certain request parameters for each piece of data if you want to send multiple requests at the same time. This uses jMeter parameterization. In actual testing scenarios, we often have such a requirement. After logging in, the token responded by the server is used as a parameter for the next request. This is the so-called parameter association.
When there are dependencies between requests, for example, the input parameter of one request is the data returned by another request, then association processing is needed. Jmeter can use the "regular expression extractor" in the "post-processor" to Handle associations.
Association is a very important technology in Jmeter tool. Because some data changes frequently during the testing process, to obtain and use this data, you must use association.
For example: after the user logs in, the session information is different. If you need to use the session for some operations, you need to save this dynamic information. There are also frequently encountered scenarios where the parameters submitted by the second request must be obtained from the return data of the first request.
2. What is association ?
What is association? In layman's terms, it means establishing a connection between requests by passing parameters. Generally, we need to use the response parameters of one request as input parameters of another request. For example, in the operation after logging in, the first step is to implement the login request, and then extract the token returned by the request and save it in a variable, which can be used as an input parameter in subsequent requests.
3. Several parameter association methods of jmeter
3.1 Regular expression extractor
1. Add a thread group to the default test plan and then add a sampler. Right-click to add a post-processor → Regular expression extractor. The regular expression extractor interface is as follows:
If you want to learn automated testing, I recommend a set of videos to you. This video can be said to be the number one automated testing tutorial on the entire network played by Bilibili. The number of people online at the same time has reached 1,000, and there are also notes that can be collected and communicated with various channels. Master technical communication: 798478386
Key parameter description:
Post-processor: plays a role when the request ends or the response result is returned
Regular Expression Extractor: Allows users to extract values from server responses by using Perl's regular expressions. This element will act on the specified range sampler, extract the required value using a regular expression, generate a template string, and store the result in the given variable name.
APPly to: scope (the assertion scope of the returned content)
Main sample and sub-samples: sampler that acts on the parent node and the sampler of the corresponding child node
Main sample only: sampler that only acts on the parent node
Sub-samples only: Sampler
JMeter Variable that only acts on child nodes: acts on jmeter variables (jmeter variable names can be entered in the input box)
Response fields to be checked: Range of response messages that need to be checked
Body: Body of the response message
(unescaped ): body, the body content of the response and replaces all html escape characters. Note that html escape characters are processed without considering the context, so there may be incorrect conversions. It is not recommended to use Body as a Document: from different
types Extract text from the file. Note that this option affects performance.
Response Headers: Response information headers
. Request Headers: Request information headers.
URL: Uniform resource locator, which is a string used to describe information resources on the Internet.
Response code: Response status code, such as 200. , 404, etc.
Response information: Response information
Reference Name: The name of the Jmeter variable, which stores the extracted results; that is, the values, fields, and variable names that need to be referenced in the next request (in the example, I extracted SOCIAL_NO)
Regular Expression: Use regular expressions to parse response results. "()" means extracting part of the value in the string. Please do not use "||" unless you need to match this character.
Commonly used regular expression operators:
Operator |
illustrate |
Example |
. |
Represents any single character |
|
[ ] |
character set, giving a range for a single character |
[abc] represents a, b, c, [az] represents a single character of az |
[^ ] |
Non-character set, giving exclusion range for single characters |
[^abc] represents a single character that is not a or b or c |
* |
The previous character is expanded zero or infinite times |
abc* means ab, abc, abcc, abccc, etc. |
+ |
The previous character can be expanded once and unlimitedly. |
abc+ means abc, abcc, abccc, etc. |
? |
The previous character is expanded 0 or 1 times |
abc? means ab, abc |
| |
Either of the left and right expressions |
abc|def means abc, def |
{m} |
Extend the previous character m times |
ab{2}c means abbc |
{m,n} |
Extend the previous character m to n times |
ab{1,2}c means abc, abbc |
^ |
Match the beginning of a string |
^abc means abc and is at the beginning of a string |
$ |
Match end of string |
abc$ represents abc and is at the end of a string |
( ) |
Only the | operator can be used inside grouping tags |
(abc) means abc, (abc|def) means abc, def |
\d |
Number, equivalent to 0-9 |
|
\w |
Word characters, equivalent to [a-z0-9A-Z_] |
Template: Represents a style referenced from a regular expression result. In fact, the result is a set, not one. $0$ represents all the results in this group, $1$ represents the first result in this group, and so on; $1$$2$ represents the first and second results in a group of regular expressions, two results. There is no gap between them; $3$, $4$ represents the 3rd and 4th results in a set of regular expressions, and there is a comma connecting the two results.
Match number: 0 represents random in the regular expression result group, 1 represents all.
Default value: Display the passed information when the reference is incorrect, and write an ERROR.
Finally, according to the above instructions, complete the configuration, and then you can add a monitor (view the result tree) first to check whether the corresponding value is obtained; for the extracted parameters, use ${sessionid_1}, ${sessionid_2} when calling.. ., if you want to get the number of matched parameters, ${sessionid_matchNr}.
3.2 Example cases
The weather in Beijing has been extremely cold recently, so Brother Hong will use the weather interface to practice. There are two interfaces as follows. Through the regular expression extractor, the city code of the first interface is passed in as a parameter of the second request.
Get city code interface;
http://toy1.weather.com.cn/search?cityname=beijing
Get weather interface based on city code:
http://www.weather.com.cn/data/cityinfo/101010100.html
Steps
1. Create an http request, obtain the city code of Beijing, and add a view result tree. As shown below:
2. After running, copy the target parameter and the characters before and after it in the response data, and try to ensure that the copied string is unique. As shown below:
3. Add a regular expression extractor and fill in the relevant parameters of the extractor. (Regular expression: "ref":"(.*?)~ ), as shown in the figure below:
4. Add a request to obtain city weather, add a view result tree, and replace 101010100 with ${citycode}. Use ${xx} to reference the parameters extracted by the above regular expression. As shown below:
5. Save and run, view the result tree, the response result is as shown below:
3.2json path postprocessor(JSON Extractor)
Usage: Extract the content from the json returned by the current interface response, and pass it as a variable in different requests. As shown below, the user id is extracted from the json returned by the login interface, and the variable name is set to id. This variable can be called directly in other requests, or used as a post parameter. Secondary plug-ins are very useful for restful interfaces.
Key parameter description:
Variable names: Name
JSONPath Expression: JSON expression
Match Numbers: Which one to match, it can be empty, that is, the first one
Default Value: The default value when no value is obtained.
For example, the return value is as follows:
{
"code": 200,
"message": "成功!",
"result": {
"apikey": "b9b3a96f7554e3bead2eccf16506c13e"
}
}
The json expression is: $.result.apikey
If the return value is an array, you need to add the position of the array, such as
{
"code": 200,
"message": "成功!",
"result": [{
"apikey": "b9b3a96f7554e3bead2eccf16506c13e"
}]
}
The json expression is: $.result[0].apikey, extract the first value.
3.3XPath Extractor
The support for association provided by jmeter includes the following two aspects:
① The ability to save the specified content on the returned page in parameters; (i.e., regular expression extractor and JSON Extractor)
② The ability to use the data in the GET or POST method Parameters to replace; (XPath Extractor)
The use of XPath Extractor is similar to the regular expression extractor (Regular Expression Extractor), except that the expression specified in the Expression is not a regular expression, but the given XPath path.
Post Processor is essentially a method of processing (post-processing) the response data received after the sampler makes a request. Post-processor components must be placed in the proper location to achieve the desired effect.
Create a new thread group, then right-click-Add-Postprocessor-XPath Extractor:
Key parameter description:
APPly to: scope (the assertion scope of the returned content)
Main sample and sub-samples: sampler that acts on the parent node and the sampler of the corresponding child node
Main sample only: sampler that only acts on the parent node
Sub-samples only: Sampler that only acts on child nodes
JMeter Variable: acts on jmeter variables (the variable name of jmeter can be entered in the input box)
XML Parsing Options: XML parameters to be parsed
Use Tidy: When the page to be processed is in HTML format, it must be selected This option; if
it is in XML or
The XML parser will use namespaces to resolve;
Validate XML: Check and parse according to the page element mode;
Ignore Whitespace: Ignore the blank content;
Fetch external DTDs: If this item is selected, the external DTD rules will be used to obtain the page content;
Return entire XPath fragment of text content: Returns the entire XPath fragment of text content;
Reference Name: Parameter that stores the extracted value.
XPath Query: XPath expression used to extract values.
Default Value: The default value of the parameter.
3.4 Boundary extractor
In the Boundary Extractor extractor, it is a small function newly introduced in version 4.0. Hong Ge personally thinks that this is more useful than regular expressions and is similar to the left and right boundaries associated with LoadRunner.
For example: When I want to register, there is a value that changes every time:
<input type="hidden" name="formhash" value="0ab4d9ec" />
Looking at this, you can see how its left and right borders are filled with name="formhash" value="11cc937d".
4. Summary
Both regular expression extractors and XPath Extractor can be used to extract specific text from a given page and save it in parameters. Both methods have their own advantages and disadvantages. The regular expression extractor can be used to extract any text on the page. The extracted content is text matching in the page content based on the regular expression; while the XPath Extractor can extract any attribute of any element on the returned page. In comparison, if the text that needs to be extracted is the attribute value of an element on the page, it is recommended to use XPath Extractor; if the position of the text that needs to be extracted is not fixed on the page, or it is not an attribute of the element, it is recommended to use regular expression extraction. device.
The difference between regular expression extractor and XPath Extractor:
① Regular expression extractor can be used to extract any text on the page, and the extracted content is text matching in the page content based on regular expressions; ②
XPath Extractor can extract the return page Any attribute of any element;
③ If the text that needs to be extracted is the attribute value of an element on the page, it is recommended to use XPath Extractor;
④ If the text that needs to be extracted is not fixed on the page, or it is not an attribute of the element, it is recommended to use regular expressions extractor.
In fact, Brother Hong thinks that it depends on your personal habits and proficiency in using it. As long as you can extract the associated parameters passed into the next request from the return result of the previous request, it can be used. Here are just a few commonly used methods.