Scripting examples on how to correlate dynamic data in your test script. Correlation is often required when using the Chrome Extension or HAR converter to generate your test script. This is because those tools will capture session IDs, CSRF tokens, VIEWSTATE, wpnonce, and other dynamic values from your specific session. These tokens typically expire very quickly. This is one of the most common things that users will script for when testing user journeys across websites or web apps.
Correlation
In a load testing scenario, correlation means extracting one or more values from the response of one request and then reusing them in subsequent requests. Often, this could be getting a token or some sort of ID necessary to fulfill a sequence of steps in a user journey.
A recording will capture session data such as CSRF tokens, VIEWSTATES, nonce, etc. This type of data is unlikely to be valid when you run your test, meaning you'll need to handle the extraction of this data from the HTML/form to include it in subsequent requests. This issue is fairly common with any site that has forms and can be handled with a little bit of scripting.
Extracting values/tokens from a JSON response
Relevant k6 APIs:
- Response.json()
- JSON.parse() (An alternative API that can be used for parsing JSON data)
Extracting values/tokens from form fields
You can choose from two different approaches when deciding how to handle form submissions. Either you use the higher-level Response.submitForm([params]) API or you extract necessary hidden fields etc. and build a request yourself and then send it using the appropriate http.* family of APIs, like http.post(url, [body], [params]).
Extracting .NET ViewStates, CSRF tokens and other hidden input fields
⚠️ Did you know?
Take note if discardResponseBodies is set to true in the options section of your script. If it is, you can either make it false or save the response per request with {"responseType": "text"} as shown in the example.
Relevant k6 APIs:
- Selection.find(selector) (the jQuery Selector API docs are also a good resource on what possible selector queries can be made)
- Selection.attr(name)
Generic extraction of values/tokens
Sometimes, responses may be neither JSON nor HTML, in which case the above extraction methods would not apply. In these situations, you would likely want to operate directly on the Response.body string using a simple function capable of extracting a string at some known location. This is typically achieved by looking for the string "boundaries" immediately before (left) and after (right) the value needing extraction.
The jslib utils library contains an example of this kind of function, findBetween. The function uses the JavaScript built-in String.indexOf and therefore doesn't depend on potentially expensive regular-expression operations.
Extracting a value/token using findBetween
Relevant k6 APIs: