This tutorial explains What is JMeter Correlation, why we need it, and how to achieve a correlation in JMeter scripts:
Correlation plays a very vital role in JMeter scripting. Let’s understand what does correlation means.
Correlation is the relationship between two or more variables. It helps in getting information for the relationship in a way that the user can decide on for future planning or action to be taken.
In this tutorial, you will learn how to achieve a correlation in JMeter scripts.
=> Click here for The Complete Free Training On JMeter (20+ Videos)
Table of Contents:
JMeter Correlation Video Tutorial
Correlation is basically extracting value from one of the requests which can be used in the subsequent request or any of the request which must be executed further.
For extracting values out from a request, you need processors. Pre-Processors like HTMLLinkParser can extract all links from a page received in response.
For the majority of the cases, post-processors like XPath and Regular Expression Extractor are used to extract values from the response and then store the value in a variable. Now, the same variable can be utilized in the next request or any request to be executed later on.
Most commonly, you would see JMeter capturing session, cooked, login credentials in some cases, and then using them for other requests as a parameter.
Understanding JMeter Correlation
This tutorial will give you a clear understanding of why and where do we need Correlation in JMeter scripting. We will also see how correlation is done with an example.
Where Do We Need Correlation
It is used when dynamic values from the response of previous requests are required to put up to further requests.
Performance testing for Static website does not require any customization, it can work with the normal functionality of record and run with the number of threads, so the correlation is not required in static websites.
These days, most of the websites have dynamic content that is returned from the webserver. For each iterative request, the dynamic response has always different data in return. To handle the same correlation is required for dynamic websites.
Why Do We Need Correlation
Correlation for Dynamic responses works in a way that it extracts the dynamic value from the response and place it in a variable which further is used by the required requests so that the script does not fail.
Example: In case a script is created for a user to log in and perform other activities on the homepage, the script will work for once only and will fail for the next time, as the user logs in for the first time session, variables get dynamically created and these session variables help for the authentication of the activities performed on the successive requests.
So, here we need to correlate web requests and dynamic variables to make the script successful, which can be done in two ways.
One way is for the developer to discover variables to be correlated and map them to be replaced in successive requests. This is a manual process.
The manual process is successful for small scripts only as they have fewer correlation points and as the number of variables to be correlated increases, the number of chances for error to occur also increases.
The large script’s correlation should be automated as it will reduce the efforts and time both and also chances of mistake will not be there.
How Correlation Can Be Done
JMeter correlation can be performed
- Using post processor “Regular expression extractor”.
- Variable to be reused wherever dynamic data is present.
Let’s go through an example of how “Regular expression extractor” is being used in correlating the variables.
Regular expression Extractor shall be added to the sampler request from which the user wants to get the value.
Sampler -> Add-> Post Processor -> Regular expression extractor.
The successive requests will be able to use a variable value to dynamically map.
Below is an example to understand the correlation in JMeter.
Steps:
#1) Add a thread group to the Test Plan.
#2) Add Sampler “HTTP Request” to the test plan and rename it Request 1 and put the Server name or IP of the website.
#3) Add “Regular expression extractor” post-processor to the test plan to extract value from the response of Sampler Request 1.
Once the extractor is added, the below values need to be filled in for extracting the value:
- Name of the created variable: It represents the name of the JMeter Variable where the extracted value will be stored.
- Regular expression: It is the expression to define the data from the response.
- Template: Templates refer to the groups of regular expressions. $1$ syntax represents group 1, similarly $2$ syntax represents group 2, and so on.
- Match Number: If 0 is provided, JMeter chooses any random match.
- Default value: Default value comes into consideration when no value matches the regular expression. User can provide the Default value as they want as “Authentication failed”, “Not Found” etc. anything that defines the error.
#4) Add another Sampler “HTTP Request “and name it Request2. Request data of this Sampler will be extracted from the response data of the first HTTP Request added.
Add the path to the request. The name of the Server or IP is not required here. The path needs to be defined with ${Name}
#5) Add Listener “View Results Tree” to the test plan to view the correlation between Request 1 and Request 2.
#6) Run the test plan and see the result. Request 2 will have some values extracted from Request1.
FAQs About JMeter Correlation
Q #1) How do you do correlation and Parameterization in JMeter?
Answer: Parameterization is used where different users in large numbers are required in a script. The different users and data can be fetched using CSV files. So, in case a script runs with a thread count as 50, every time it will fetch a new user detail from the CSV file.
To handle the Correlation “Regular expression extractor” post-processor is used to extract the value of the variable from the first request for the successive request.
Parameterization looks out for input values of the user whereas Correlation looks out for data that is returned by the server for the request created.
Q #2) How does JMeter handle dynamic values?
Answer: To handle JMeter dynamic values two steps need to be performed i.e. one to get and store the dynamic value from the response and second to use that value in the successive request. This is done using the Regular expression extractor post-processor.
Q #3) How does JMeter correlate header values?
Answer: To correlate header values in JMeter “Regular expression extractor” post-processor is required.
- In that Apply to – “Main sample” and for the field to check “Response headers” has to be selected.
- Put the values for all the fields.
- The name provided in name of the created variable will have the value of the variable stored in JMeter. Provide expression in the Reference expression section, it will pick the actual value of the response header parameter.
- Provide template name as $1$.
- Now to get the value use $ and variable name in braces like $ {Variable name}
Conclusion
Correlation is a very important aspect of JMeter. Static websites do not require correlation, but dynamic sites require correlation as the server responds to dynamic variables that keep on changing every time. to get the scripts reused again and again the correlation between the variable values is required.
That’s why “Regular expression extractor” is used to extract variable value from one request to be used in successive requests.