Skip to content
Swarmetry

Build scenarios

Sessions and test data

Log in once per virtual user, keep values between iterations, generate test data.

Real users log in once and then browse for a while; they type different values every time. This page shows how to build that without code: log in once per virtual user, keep values between iterations, and generate test data.

Log in once per virtual user

In the workflow editor, drag Once per virtual user from the palette and put the login steps inside.

Once per virtual user

  • The steps inside run in the first iteration of each virtual user, and are skipped afterwards. (An iteration is one pass of a virtual user through its flow, from the first step to the last; a virtual user repeats its flow until the test ends.)
  • Every value saved inside (an extracted token, a variable you set) is kept: the steps after the block, and the next iterations, can use it.
  • Cookies are kept too: a session cookie received by the login stays with the virtual user for its whole test, also in browser steps.
  • If the block did not complete — an error, a failed check, a value that could not be extracted — it runs again at the next iteration.

Run again when the session expires: choose one or more rules in the panel.

Rule Example Use it when
After a duration 15 min the token lives 15 minutes
Every N iterations 100 the application ends sessions after some activity
On response statuses 401, 419 the server answers 401 when the session has expired: the next iteration logs in again
When a condition holds tokenExpiresAt < now the login returned when the token expires

A block inside a loop still runs once per virtual user, not once per pass of the loop (a problem tells you so). JMeter plans are imported accordingly: a Once Only Controller directly in a thread group becomes this block; one inside a loop becomes a condition on the first pass of that loop.

Keep a value between iterations

In the scenario settings, a text, number or environment variable can be marked Keep between iterations: its value at the end of an iteration becomes its value at the start of the next one, for the same virtual user (the declared value is the starting value). Secrets and dataset columns are never kept: they are read again at every iteration.

Generate test data

Type two opening braces in any field to see what you can insert: variables, dataset columns, environment variables, built-in values and functions. Choose a function to fill its arguments in a small form.

Functions

Function Gives Example
$randomInt(min, max) a whole number between min and max {{$randomInt(1, 100)}} → 42
$randomDecimal(min, max, decimals) a decimal number {{$randomDecimal(1, 50, 2)}} → 17.31
$randomString(length, characters) random text (letters and digits by default, or your own characters) {{$randomString(8)}}
$randomItem(a, b, …) one of the values, or one element of a list variable {{$randomItem("red", "green")}}
$date(format, offset, time zone) the current date and time, shifted and formatted {{$date("YYYY-MM-DD", "+1d", "Europe/Paris")}} → tomorrow
$counter(name, start, step) 1, 2, 3… for each virtual user {{$counter("order")}}
$length(value) the length of a text or a list {{$length(items)}}
$base64Encode, $base64Decode, $urlEncode, $urlDecode, $upper, $lower encodings and case {{$base64Encode("user:pass")}}
$md5, $sha1, $sha256, $sha384, $sha512 a hash (a fixed-length fingerprint of the value) {{$sha256(body)}}
$hmac(algorithm, key, data) a signature with a key, which can be a project secret {{$hmac("sha256", secret:SIGNING_KEY, body)}}

Good to know:

  • Dates use fields such as YYYY MM DD HH mm ss; put other letters between square brackets: YYYY-MM-DD[T]HH:mm. The time zone is UTC unless you choose one (Europe/Paris, +02:00).
  • Counters belong to each virtual user (virtual users share no memory). For values unique in the whole test, use $uuid or $iteration.
  • $vuIteration (0, 1, 2… for the virtual user) and $vuIdInInstance are built-in values, like $vu and $uuid.
  • A secret is accepted only as the key of $hmac: it is read when the request is sent and never shown, stored or logged. The signature itself can be used anywhere, in a URL too.

JMeter functions (__Random, __RandomString, __time, __timeShift, __counter, __urlencode, __changeCase, __digest, __base64Encode, __iterationNum) and the Postman variables $randomInt, $isoTimestamp and $randomAlphaNumeric are converted when you import a plan or a collection.

Ask the assistant

The assistant knows worked examples of everything on this page. Describe what you need in your own words, for example “in Shop session, log in once per virtual user as alice, the password is in the secret ALICE_PASSWORD” or “order a random quantity between 1 and 5 with a reference made of today's date and 6 random letters”: it adds the steps and the functions for you. Review the result in the workflow editor before you run a large test.