Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Parameterization and Correlation

Correlating a Login Flow Step by Step

Build a realistic JMeter login flow: HTTP Cookie Manager, CSRF token extraction, bearer token reuse, redirects and per-user data, with a runnable server stand-in.

Login is the first thing almost every load test must get right, and the first place where tests silently go wrong. If login fails, every later request is rejected quickly, response times look fantastic, and the report is worthless. This lesson combines what you have learned about CSV data, variables and extractors into two complete flows: a session-cookie website with a CSRF token, and a token-based API.

JMeter cannot run on this site, so the test-plan structure is described in text and the server behaviour is imitated with a small Python stand-in.

A typical server-rendered site works like this:

sequenceDiagram
    participant U as Virtual user
    participant S as Server
    U->>S: GET /login
    S-->>U: HTML form with hidden csrf field + Set-Cookie session
    U->>S: POST /login (username, password, csrf) + cookie
    S-->>U: 302 redirect to /account
    U->>S: GET /account + cookie
    S-->>U: 200 account page

The server ties the CSRF token to the session cookie, so both must come from the same GET.

The test plan tree looks like this:

TEXT
Test Plan
  User Defined Variables           (host, protocol)
  HTTP Request Defaults            (${protocol}://${host})
  HTTP Cookie Manager              (clear cookies each iteration = true)
  CSV Data Set Config              (users.csv: username,password)
  Thread Group
    Transaction Controller: Login
      HTTP Request  GET  /login
        CSS Selector Extractor    input[name=csrf] -> csrf   (attribute: value)
        Response Assertion        contains "Sign in"
      HTTP Request  POST /login   username=${username}&password=${password}&csrf=${csrf}
        Response Assertion        response code 200 (after redirect)
    HTTP Request  GET /account
      Response Assertion          contains "Welcome, ${username}"

Key points:

  • HTTP Cookie Manager is mandatory. Without it every request is a stranger. Tick Clear cookies each iteration so one iteration equals one new browser.
  • Follow Redirects should be on for the POST so the final page is checked. If you turn it off, you assert on the 302 instead.
  • The assertion on the last page proves you are really logged in. A login page returns 200 too, so check for text that only appears when logged in.
  • HTTP Request Defaults supplies protocol, host and port, so samplers only hold paths.

Flow B: token-based API

APIs usually return a token in JSON and expect it in a header:

TEXT
Thread Group
  HTTP Request POST /api/login   body: {"email":"${username}","password":"${password}"}
    HTTP Header Manager          Content-Type: application/json
    JSON Extractor               $.token -> token   (default TOKEN_NOT_FOUND)
    JSON Assertion               $.token exists
  HTTP Header Manager            Authorization: Bearer ${token}      (placed after the login sampler)
  HTTP Request GET /api/orders

An HTTP Header Manager placed directly under the Thread Group applies to all samplers in that scope, including the login request (which does not have a token yet). Put the Authorization header inside the later samplers or in a Transaction Controller that follows login, or add it with a JSR223 PreProcessor.

Running login once or every iteration

  • Once per user: put the login sampler inside a Once Only Controller. The thread logs in on its first iteration only and reuses the cookie or token afterwards. Realistic for long sessions.
  • Every iteration: put login in the normal flow. Useful when you want to stress the login service itself, but it inflates the number of login requests compared with real traffic.

Decide from the production traffic shape, not from convenience. Most visitors log in once and perform many actions.

A stand-in server and client

The next block imitates the cookie plus CSRF protocol and runs a client that correlates it. It shows what the JMeter plan above does: first GET, extract the token, then POST with it. The server code is a stand-in for your application, not JMeter.

Python Stand-in: login with session cookie and CSRF token

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.

The uncorrelated request fails with 403: exactly what a replayed recording does. With correlation it succeeds, and a wrong password gives 401, which a good assertion should also catch.

Verifying before you scale

Always debug with one thread, one iteration and a View Results Tree listener showing request and response. Check:

  1. The first response contains the token you expect.
  2. The POST body shows the substituted values, not ${csrf}.
  3. The final page contains logged-in text.
  4. Then try 5 users with 5 different accounts and confirm they do not log each other out (some systems allow only one session per account).

Disable View Results Tree during real load runs: it stores every response in memory.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a replayed recording with a fixed CSRF token fail?

    The token belongs to one session, so each virtual user must extract a fresh token from its own GET.
  2. 2. Which element gives each virtual user its own cookie session?

    The HTTP Cookie Manager stores cookies per thread; tick Clear cookies each iteration for a fresh browser each loop.
  3. 3. You want each user to log in only on the first iteration. What do you use?

    The Once Only Controller runs its children on the first iteration of each thread only.
  4. 4. What proves that login really succeeded?

    Failed logins can also return 200 with a login page, so check for content such as the welcome text.

Summary

  • Login needs a cookie manager, extractors for dynamic values and assertions that prove success.
  • Cookie sessions bind the CSRF token to the cookie; token APIs return a bearer token in JSON.
  • Use a Once Only Controller for a login that happens once per user.
  • Debug with one thread and View Results Tree before scaling, then disable the listener.

Next: Distributed Testing Architecture