No results for

Powered byAlgolia

Correlation and Dynamic Data

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

extract-json.js
1import http from 'k6/http';
2import { check } from 'k6';
3
4export default function () {
5 // Make a request that returns some JSON data
6 const res = http.get('https://httpbin.test.k6.io/json');
7
8 // Extract data from that JSON data by first parsing it
9 // using a call to "json()" and then accessing properties by
10 // navigating the JSON data as a JS object with dot notation.
11 const slide1 = res.json().slideshow.slides[0];
12 check(slide1, {
13 'slide 1 has correct title': (s) => s.title === 'Wake up to WonderWidgets!',
14 'slide 1 has correct type': (s) => s.type === 'all',
15 });
16
17 // Now we could use the "slide1" variable in subsequent requests...
18}

Relevant k6 APIs:

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

extract-from-hidden.js
1import http from 'k6/http';
2import { sleep } from 'k6';
3
4export default function () {
5 // Request the page containing a form and save the response. This gives you access
6 //to the response object, `res`.
7 const res = http.get('https://test.k6.io/my_messages.php', { responseType: 'text' });
8
9 // Query the HTML for an input field named "redir". We want the value or "redir"
10 const elem = res.html().find('input[name=redir]');
11
12 // Get the value of the attribute "value" and save it to a variable
13 const val = elem.attr('value');
14
15 // Now you can concatenate this extracted value in subsequent requests that require it.
16 // ...
17 // console.log() works when executing k6 scripts locally and is handy for debugging purposes
18 console.log('The value of the hidden field redir is: ' + val);
19
20 sleep(1);
21}

⚠️ 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:

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

extract-findBetween.js
1import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
2import { check } from 'k6';
3import http from 'k6/http';
4
5export default function () {
6 // This request returns XML:
7 const res = http.get('https://httpbin.test.k6.io/xml');
8
9 // Use findBetween to extract the first title encountered:
10 const title = findBetween(res.body, '<title>', '</title>');
11
12 check(title, {
13 'title is correct': (t) => t === 'Wake up to WonderWidgets!',
14 });
15}

Relevant k6 APIs: