Correlation in JMeter

What is correlation and why it is required?

Correlation is the most important aspect of scripting. It generally includes fetching dynamic data from preceding requests/calls and posting it to the subsequent requests.
Let's take an example to find out why exactly we need correlation. Suppose we have recorded a scenario in which -
->User enters login details and click OK button
->Home page opens and user take further actions

Now, if we just playback this script, the test will fail even for a single user. This is because of the authentication mechanism used. When we login to a website, session variables are dynamically created. These session variables are passed to the subsequent requests and help validation and authentication of the actions performed. So, one cannot just record and playback the requests having these variables. Here, we need to correlate the web requests with the dynamic variables. And for correlation, we need to use the "Regular Expression Extractor" which makes use of regular expressions.

A brief insight to regular expressions

Regular expressions are used to fetch data from a string based on a search pattern. Basically, what we do is, in order to extract any value (generally a dynamically created value) from a string response, we define a left bound of the variable then some wildcard characters and then right bound-

(Left Bound)(Wildcard Characters)(Right Bound)

E.g. if we have text response like-

...__EVENTVALIDATION"value="weriudflsdfspdfusdjhsdihffgdfgpdfjsdjfpj" />...

And we need to extract the value of Event validation variable using regular expressions; the regular expression for the same will be-

__EVENTVALIDATION" value="(.+?)" />
	where, Left Bound = __EVENTVALIDATION" value="
	Wildcard characters = (.+?)
	Right Bound = " />

If you do not want to get deeper into regular expressions, then the wildcard characters (.+?) would suffice in most of the cases. For more information on regular expressions and meaning of each wild card character visit Regular-Expression info Website.

Regular Expression Extractor

Coming back to JMeter, consider an example where we have two operations-

  1. User launch website
  2. User fill details and click on OK button

Now, the call user launch website creates a dynamic variable event validation that we can check in Response Data tab of "View Result Tree" listener for the call. The value of this variable is then passed to subsequent call related to "User fill details and click on OK button" as Http post parameter.

Steps for correlating the Event validation values-

  1. Run the script containing the both the above stated operations.
  2. Go to Response tab (Text mode) in "View Result Tree" listener of "User launch website" operation. Refer to the below image, we can see that the second operation "User fill details and click on OK button" is in red because it is not yet correlated.

3. Create a Regular expression for extracting the value of Event validation variable's value. As stated above the R.E. for this will be-
__EVENTVALIDATION" value="(.+?)" />

4. Go to http request under "User Launch Website" transaction controller-> Add -> Post Processor -> Regular Expression Extractor.

Correlation Adding "Regular Expression Extractor" control

Correlation R.E. Extractor Parameters Filled

5. The reference name inserted is the name of the variable created that will capture the Event validation value generated by the http request under "User launch website" operation.

6. Now pass this variable to the subsequent http request under "User fill details and click on OK button" as post request- overriding the already present hardcoded value of Event Validation variable.

Correlation Request without correlation (Hardcoded values)

Correlation Request with correlation (Dynamic values)

7. Run the Test plan again. All green! That's it.
This was all about correlation. In order to get good understanding of correlation (or scripting for that matter) we need to have a good understanding of two things- the dynamic variables generated by the programming languages and Regular expressions.