FAQs »

What happens to my cookies and variables between different iterations of the same bot?

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.

Clear cookies checkbox
Clear cookies checkbox

Enabling the setting

This setting is enabled by default, so each bot 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 bot’s state preserved between iterations of the same bot.

For example, you might have a situation where you want each bot 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 bot
if (user.getIteration() === 0) {
    // Grab the next row from the Account dataset
    const account = user.getVariable("Account");

    // Send a POST to the login endpoint
    http.post(
        "https://petstore.loadster.app/api/login",
        {
            username: account[0],
            password: account[1]
        },
        {
            validators: [
                function (response) {
                    const body = JSON.parse(response.string());

                    // Capture the resulting API token
                    // and store for later use by this bot
                    user.setVariable("ApiToken", body.token);
                }
            ]
        }
    );
}

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

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