Skip to main content

Correlation in JMeter with an Example

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

    The user enters login details and clicks the OK button

      The home page opens and the user takes 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 log in 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 into 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 (text response), we define a left bound of the variable than some wildcard characters and then a right bound (Left Bound)(Wildcard Characters)(Right Bound)

      For example: If we have a text response like;

      .......__EVENTVALIDATION"value="weriudflsdfspdfusdjfsisdpfjpsdfohsdihffgdfgpdfjsdjfpj" />...

      And we need to extract the value of the 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 cases.

      For more information on regular expressions and meaning of each wild card character visit;

      http://www.regular-expressions.info/tutorialcnt.html 

      Regular Expression Extractor

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

        The user launch website

        The user fills in details and clicks on the OK button

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

        Steps for correlating the Event validation values

          Run the script containing both the above-stated operations

          Go to the Response tab (Text mode) in the "View Result Tree" listener of the "User launch website" operation. BTW, we see the second operation "User fill details and click OK button" in red because it is not yet correlated

          Create a Regular expression for extracting the value of the Event validation variable's value. As stated above the R.E. for this will be-

          __EVENTVALIDATION" value="(.+?)" />

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

          Adding "Regular Expression Extractor" control

          R.E. Extractor Parameters Filled

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

          Now pass this variable to the subsequent HTTP request under "User fill details and click OK button" as a post request- overriding the already present hard-coded value of the Event Validation variable.

          Request without correlation (Hardcoded values)

          Request with correlation (Dynamic values)

          Run the test plan again. All green? That's it.

          This was all about correlation. In order to get a 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.

          SPREE APP

          Let us take a working example of a Spree App where we will be recording the application and then correlating it

          The URL for this Spree App is given below :

          http://52.42.228.15:8888/

          We will be using the above URL to record the below actions

            Navigating to the home page

            Navigating to the Login page

            Entering credentials and clicking on Submit

            Clicking on ruby-on-rails-tote

            Adding to Cart

            Clicking on Products

            Clicking on apache-baseball-jersey

            Adding to Cart

            Clicking on MyAccount

            Navigating to the Home page

            Logging out

            Actions from Step 5 to Step 7 may be repeated depending on the requirement of the user. Once the above action is recorded user runs the recorded and checks the Output as shown below.

            As you can see from the above screen-shot there are a few requests like “Submit”, “My Account” and “Logout” that have failed.

            By Checking the “Login Page” response data we find that there is an authentication token that is generated that is unique and should be used for all the failed transactions. The screenshot is given below.

            As shown above we will have to use the Regular Expression extractor to extract the token and to be used in the Submit transaction.

            The Reference name should be noted and the same should be used in the Submit transaction as shown below.

            Replace the same in the transaction which needs the Authenticity token. Save the script and run the test again.

            Since we have extracted the token and used it in the transactions wherever necessary all the steps would now pass.


            Last updated: July 05, 2023

            If you have any feedback on this topic, please contact us at documentation@appvance.ai