How to do Correlation in JMeter?


How to do correlation in Jmeter?

The process is simple, In order to make you understand, I will split the correlation process into 5 steps.

1.) Identify the dynamic string
2.) Find it in the server's response
3.) Prepare a regular expression
4.) Extract the string from response
5.) Substitute the correlation.

NOTE: Let's assume that we are going to correlate a string Viewstate .



Step 1.) Identify the dynamic string:

You must need to know, which value needs to be correlated. Unless and until you identify the dynamic string, you cannot proceed ahead in this aspect. In order to identify a sting whether it is dynamic or not, You can record the script for 2 times and check if the same string is changing or not . Else, instead of recording you can check the same using Chrome developer option "Hit F12 --> Select Network Tab --> Perform the action --> select a request where string is present --> Find the string under query string parameters".

Outcome: Let's say you have successfully identified the string (Viewstate) which needs to be correlated.

Image:



Step 2.) Find it in the server's response:


Once you identified the string, Copy it and search it in the response of a specific request where it is generated. Usually, if we see a dynamic string, we can get it in it's just preceding request's response. For example, if you find a dynamic string in transaction 3, there are more chances to find it in the response of 2nd transaction.

Outcome: Let's say you found that Viewstate is being passed inside the 3rd transaction and it is actually getting generated in the response of 2nd transaction.

Image: 



Step 3.) Prepare a regular expression: This is the process where you need to prepare a regular expression. A regular expression is nothing but a sequence of character that matches the strings or string pattern. This can also be called as "RegEx". For making this step even simpler, I would like to give you a formula. Using this, you can prepare the regular expression.

Formula --> Left Boundary(.*?)Right Boundary; Now applying this formula to the found string. Do not take just the string, we need to take the string along with its left and right boundary which means some characters present left to the string and some characters present right to the string.

             VIEWSTATE>; "Value"="eyn74nvh3hjebhrfvbhv984kmnvnj=="; (You can refer above image to understand from where I copied this)

Where the string highlighted in the blue color is required sting (eyn74nvh3hjebhrfvbhv984kmnvnj==).
The string highlighted in the red color is left boundary (VIEWSTATE>; "Value"=")
The string highlighted in the green color is right boundary (";)



How to use this formula?

Now you just need to replace the required string with regex . See below

VIEWSTATE>; "Value"="(.*?)”;

Yes, I just have replace the dynamic string with (.*?).  whatever new id is getting generated each time that will be always there in between left and right boundary. So (.*?) captures the dynamic id with the help of left and right boundary.

Once you prepare the expression, you need to check this expression to ensure that it captures the required string. To do so, Just keep your prepared expression in search filed and click on “Regular expression” check box which is present in the lower right corner in the view results tree itself and then click on Find next. If your regular expression works, then Jmeter should highlight the Viewstate value. See the image below.

Outcome: We have successfully prepared the regular expression and checked whether it is working or not.

Image:



Step 4.) Extract the String:

Now using the expression which we prepared in step 3, we are going to extract the string. This process deals with the extracting the required string from response and storing it into the variable so that we can substitute the variable in place of where Viewstate is passing. To do so, you need to add a Regular Expressin Extractor.

This can be added from Post Processors → Regular Expression Extractor.

Please note that this extractor must be added to the request where the String is getting generated not where it is passing. In our case, we found this string getting generated in the response of 2nd transaction  so we must need to add  Regular Expression Extractor to the 2nd transaction not to the 3rd transaction.

Image:



Once you add the extractor, You will need to fill the following fields.


Reference Name:
Regular Expression:
Template:
Match No.(0 for Random):
Default Value:

Before filling these fields, You need to know what they are

Reference Name:  This is just a variable name which you are going to declare. The string captured by your regular expression will be stored in this variable name. It can be anything but it is good to have a meaningful name always.

Regular Expression: This is nothing but the same expression which you prepared in step 3. Just copy the exact expression from step 3 and paste it here.

Template: This is actually the order of group. Here we can represent (.*?) as a group. Suppose, if you used two (.*?) in one regular expression then you need to give $1$$2$ in this field.
But in our case, we just want to capture one value so we just gave one (.*?). So it is sufficient to fill this field with $1$.

Note: You can learn more on this in the upcoming topics.

Match No.(0 for Random): Suppose if your regular expression is finding the Viewstate at first match only (after clicking on Find button once ) then the match no here is entered to be 1. If the expression is matching for mutliple times and you are getting your required value at 3rd match then you should give match No as 3. If all the matches are giving same value then you can simply enter 0 here which will take any value randomly (out of all matches.)

Default Value:  The text you enter here will be shown in the response when your regular expression is failed to find any string. The good practice would be entering Not found followed by your variable Name. Example: Viewstate_Not_Found

Outcome: You have successfully set an extractor which will store the dynamic id into the variable

Image:



Step 5.) Substitute the variable:

Once you extracted the dynamic string to a variable, we need to substitute this variable with the dynamic string in the requests. Now, In our case, Vewstate is the the dynamic string and it is passing in 3rd transaction. So we need to substitute this Viewstate value with new variable.

In Jmeter, there is a syntax to substitute the variable. It is ${VARIABLE}. Here VARIABLE needs to be replaced with your actual variable name.

Outcome: You have successfully substituted the variable with the hard-coded value.

For better understanding, see the below image.



That's it. You have completed the correlation process. To ensure we successfully correlated, we need to run the script again and check below things

1.)    Whether a new id has been generated for  Viewstate
2.)    whether our expression captured the newly generated  Viewstate
3.)    Whether our extractor stored the new id to our variable  Viewstate
4.)    Whether the new id is passed in 3rd transaction.

For checking these 4 points, I re-run my script and took screen-shots. Please see below.

Image for point 1.)




 Image for point 2.)



Image for point 3&4.) 



                                                                        THANK YOU
                                                               :)

No comments:

Post a Comment