Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Parameterization and Correlation

Extractors and Correlation in JMeter

Capture dynamic values such as tokens, CSRF fields and ids with the Regular Expression, JSON, CSS Selector and Boundary extractors, and reuse them in later requests.

Real applications return values that change with every response: a session token, a CSRF field, the id of the order you just created. If a recorded script replays the old value, the server answers with 401, 403 or 400, and your test measures error pages instead of the application. Capturing a value from one response and using it in the next request is called correlation.

In JMeter, correlation is done with post-processors, which run after a sampler and store a result in a variable. This lesson shows the four extractors you will use most and a Python stand-in for each idea. JMeter cannot run here, so the menus and field names are described exactly while the runnable code only imitates the matching.

Where extractors go

Add an extractor as a child of the sampler whose response contains the value: right-click the sampler, then Add > Post Processors. The extractor then applies to that sampler only. Placed at Thread Group level, it would run after every sampler in that group, which is slow and usually wrong.

The extracted value is stored in a variable you name, and you use it later as ${token}.

JSON Extractor

For JSON APIs this is the first choice. Its fields:

Field Meaning
Names of created variables token
JSON Path expressions $.data.token
Match No. 1 for the first match, 0 for random, -1 for all
Default Values TOKEN_NOT_FOUND

Always set a Default Value. Without one, a failed extraction leaves the old value in place (or nothing), and the error shows up later in a confusing place. With a default, you can add an assertion that fails the sampler when ${token} equals TOKEN_NOT_FOUND.

Useful JSON Path patterns: $.items[0].id (first element), $..id (every id anywhere), $.items[?(@.status=='open')].id (filtered).

Regular Expression Extractor

Use it for HTML, headers and any text. The important fields are Field to check (response body, headers, URL), Regular Expression, Template and Match No.

TEXT
Regular Expression: name="csrf" value="(.+?)"
Template:           $1$
Match No.:          1
Default Value:      CSRF_NOT_FOUND

The text in parentheses is a capture group. $1$ in the template means "use the first group". Prefer the lazy .+? over the greedy .+, or the match can run to the end of the line and swallow several fields.

Python Stand-in: a regex extractor with Match No. and defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

CSS Selector Extractor and Boundary Extractor

  • CSS Selector Extractor (named CSS Selector Extractor in the Post Processors menu): fields are the CSS selector such as input[name=csrf], an attribute name such as value, and a match number. It is the cleanest choice for HTML because it ignores whitespace and attribute order.
  • Boundary Extractor: you give a left boundary and right boundary. JMeter takes the text between them, with no regex syntax to get wrong. It is also faster than a regex on huge responses.
TEXT
Left boundary:  "sessionId":"
Right boundary: "

Using the value

Once stored, use ${token} anywhere: in a header (HTTP Header Manager with Authorization: Bearer ${token}), a URL path (/orders/${orderId}), or a body parameter. That is the whole job of correlation.

Python Stand-in: JSON path extraction and reuse in the next request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Finding what to correlate

  1. Record the flow with the HTTP(S) Test Script Recorder or write the samplers by hand.
  2. Replay it with one user and a View Results Tree listener. Look for responses with 401, 403, 419, 400 or a redirect to the login page.
  3. Compare two recordings of the same flow (use a text diff). Values that differ between runs are candidates: tokens, timestamps, ids, nonces, __VIEWSTATE.
  4. Add an extractor to the response that first contains the value. Replace the recorded value in later requests with ${variable}.
  5. Add an assertion on each step to prove the flow really worked (a Response Assertion that checks for expected text is enough).

Common mistakes

  • Extracting from the wrong sampler or in the wrong order. The extractor must belong to the response that contains the value.
  • A greedy expression such as value="(.*)" that captures far more than intended.
  • No Default Value, so failed extractions go unnoticed.
  • Match No. 0 (random) when you meant the first match.
  • Extracting from the page body when the token is in a response header.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should a Regular Expression Extractor usually be placed?

    Post-processors apply to the sampler they are attached to. Scoping keeps them fast and correct.
  2. 2. Why set a Default Value on an extractor?

    A value such as TOKEN_NOT_FOUND lets an assertion detect a failed extraction instead of silently using stale data.
  3. 3. Which template extracts the first capture group?

    $1$ is the first group in parentheses; $0$ would be the whole match.
  4. 4. Which extractor is best for a JSON API response?

    The JSON Extractor understands JSON Path expressions such as $.data.token.

Summary

  • Correlation captures dynamic values from responses and reuses them with ${name}.
  • Use the JSON Extractor for JSON, CSS Selector or Boundary for HTML, and Regular Expression for anything else.
  • Attach each extractor to the right sampler and always set a Default Value.
  • Find candidates by comparing recordings and watching for 401, 403 and redirects.

Next: Correlating a Login Flow