What happens to my cookies and variables between different iterations of the same v-user?

Whether or not cookies and variables remain in scope between iterations depends on a setting called Clear cookies and variables after each iteration. You can enable or disable it on your Settings page.

Enabling the setting

This setting is enabled by default, so each v-user gets a fresh state for every iteration. All cookies and variables from the previous iteration disappear. It’s like a whole new person or client is hitting your site each time. In most cases this is desirable, because the total number of iterations in your load test will correlate with the total number of unique user sessions, and you’ll avoid the potential confusion of leftover state from the previous iteration of your script.

Disabling the setting

Occasionally you’ll want to do some fancy looping or something, and will want the v-user’s state preserved between iterations of the same v-user.

For example, you might have a situation where you want each v-user to log in on their first iteration, and then repeat some task over and over throughout the duration of the test, without logging in again.

You could accomplish that by disabling the Clear cookies and variables after each iteration setting, and adding a code block to the beginning of your script that contains a bit of logic to only log in to your application if it’s the first iteration.

// Submit the login form but only if it's the first iteration of this v-user
if (user.getIteration() === 0) {
    // Grab the next row from the Account dataset
    var account = user.getVariable("Account");
    
    // Send a POST to the login endpoint
    user.post(
        "https://slothereum.cc/api/login", 
        { 
            username: account[0], 
            password: account[1] 
        }, 
        {
            capturers: [
                function (response) {
                    var body = JSON.parse(response.bodyAsString);
                    
                    // Capture the resulting API key and store for later use by this v-user
                    user.setVariable("ApiKey", body.apiKey);
                }
            ]
        }
    );
}

The important part here is user.getIteration() === 0 which checks if we’re currently on the first iteration for this v-user. In this example, we’re only logging in on the first iteration, and then saving the API key for use by subsequent steps in this iteration as well as subsequent iterations of this same v-user.

Cookies are also kept in scope automatically if there are any.