添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
淡定的墨镜  ·  ULK 进程 - rqdmap | blog·  3 周前    · 
纯真的橙子  ·  jpa groupby - CSDN文库·  3 月前    · 
豁达的帽子  ·  Home | Team Focus·  4 月前    · 

Table of Contents

What is Correlation?

There is no official term as correlation in JMeter ecosystem. It is a generic term used by performance engineers/testers. Also, entitling this blog post with a correlation word helps in SEO :)

Correlation is a process of extracting a string from the response body, response header or basically anything from the response. After extracting the response, it can be stored in a variable for subsequent use.

Typical example would be a login session. Session IDs are unique and gibberish. Hard-coding it in the script is not an effective way of handling it. Because it may expire based on the web server-side properties.

Correlation – Hard way in JMeter

Let us start with extracting a title tag from the response using Groovy scripting by adding a JSR223 Post processor in JMeter. Below is the test plan tree.

response = prev.getResponseDataAsString() //Extract the previous response
def extractTitle = /<title>(.+?)<\/title>/
def matcher = response =~ extractTitle
if (matcher.size() >=1) {
    println matcher.findAll()[0][1]
    vars.put("extractTitle",matcher.findAll()[0][1])

Here is the URL https://jpetstore-qainsights.cloud.okteto.net/jpetstore/actions/Catalog.action

The first step is to read the HTTP response as a string using prev.getResponseDataAsString()

prev is an API call which extracts the previous SampleResult. Using the method getResponseDataAsString() we can extract the whole response as a string and store it in a variable.

The next two lines define our regular expression pattern and the matching conditions. Groovy comes with powerful regular expression pattern matching.

def extractTitle = /<title>(.+?)<\/title>/
def matcher = response =~ extractTitle

The next block checks for any matches of >=1, then it will print the extracted string from the array list. Then, it will store the value to the variable extractTitle using the vars.put method.

if (matcher.size() >=1) {
    println matcher.findAll()[0][1]
    vars.put("extractTitle",matcher.findAll()[0][1])

Here is the output:

The above method is not effective for a couple of reasons. One, the array index to capture the desired string might be cumbersome for the complex response. Second, typically the pattern we use here is apt for the text response, not for the HTML response. For the complex HTML response, using the regular expression might not yield better performance.

import org.jsoup.nodes.Document import org.jsoup.nodes.Element import org.jsoup.select.Elements @Grab(group='org.jsoup', module='jsoup', version='1.15.2') response = prev.getResponseDataAsString() // Extract response Document doc = Jsoup.parse(response) println doc.title()

doc object will parse the response and print the title to the command prompt in JMeter.

To print all the links and its text, the below code snippet will be useful.

import org.jsoup.nodes.Document import org.jsoup.nodes.Element import org.jsoup.select.Elements @Grab(group='org.jsoup', module='jsoup', version='1.15.2') response = prev.getResponseDataAsString() // Extract response Document doc = Jsoup.parse(response) println doc.title() // To print all the links and its text Elements links = doc.body().getElementsByTag("a"); for (Element link : links) { String linkHref = link.attr("href"); String linkText = link.text(); println linkHref + linkText

To print all the list box elements and random list box values for the url (http://computer-database.gatling.io/computers/new), use the below code snippet.

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
@Grab(group='org.jsoup', module='jsoup', version='1.15.2')
response = prev.getResponseDataAsString() // Extract response
companyList = []
Random random = new Random()
Document doc = Jsoup.parse(response)
// To print all the list box elements
Elements lists = doc.body().select("select option")
for (Element list : lists) {
    println "Company is " + list.text()
    companyList.add(list.text())
// To print random list box element
println("The total companies are " + companyList.size())
println(companyList[random.nextInt(companyList.size())])

Final Words

As you learned, by leveraging the prev API we can extract the response and then parse it using JSoup library or by writing desired regular expressions in a hard way without using the built-in elements such as Regular Expression Extractor or JSON Extractor and more. This approach might not save time, but it is worth learning this approach which comes handy in situations like interviews.

Categories Performance Testing

I am NaveenKumar Namachivayam, a performance engineer. My mission is to help and transform manual/automation testers into performance testers and engineers. Also, I love to develop crappy apps, create YouTube tutorials and write about the current trends in performance testing. Thanks for stopping by.

  • Performance Testing using TruWeb - Udemy Course
  • Learn JMeter - Udemy Course
  • Earn Cash by Performance Testing (2018)
  • Excel in MS Excel
  • The Complete Guide on HP LoadRunner Web Controller (Kindle Edition)
  • © NaveenKumar Namachivayam | 2024 | All Rights Reserved.
    This disclaimer informs readers that the views, thoughts, and opinions expressed in the text belong solely to the author, and not necessarily to the author’s employer, organization, committee or other group or individual. I may receive a small commission for the purchases made through my links.