How do I parameterize my k6 test

Hi, I’ve got a new problem to solve.

I want every VU to represent a specific person, with different login credentials.
In other words, I want my 5 VUs to authenticate as 5 different people, and run separate scenarios.
Is it possible to perform such task with k6?

Thanks in advance !

1 Like

Hi,

yes, each VU can run completely separate js code. take a look at this example:

import http from "k6/http"

export function setup() {
    return {
        1: {
            token: 'value of the token for vu1',
            endpoint : '/endpoint/for/vu1'
        },
        2: {
            token: 'value of the token for vu2',
            endpoint : '/endpoint/for/vu2'
        },
        // ...
    }
}

const baseURL = "http://google.com"

export default function(data) {
  const url = baseURL + data[__VU]["endpoint"];
  const options = {
    headers: {
      "X-Token-header-name": data[__VU] ["token"]
    }
  };

  const response = http.get(url, options);
}
1 Like

2 posts were split to a new topic: How to distribute VU’s across different scenarios with k6

How would I create a variable to replace the authentication token cookie so that all the calls don’t use the same token from the browser har test, this is what the code looks like(with some confidential stuff taken out)

response = http.get(

        "https://bi.dev..........

        {

          headers: {

            "user-agent":

              "Mozilla/5.0 (Windows NT........)

            accept: "*/*",

            "sec-fetch-site": "same-site",

            "sec-fetch-mode": "no-cors",

            "sec-fetch-dest": "script",

            referer:

              "https://...............",

            "accept-encoding": "gzip, deflate, br",

            "accept-language": "en-US,en;q=0.9",

            cookie:

              "access_token_dev=eyJraWQiO..................
}.
}
);

Cookies should be mostly automatically managed by k6, though you can also manage t hem manually, if you want to: https://k6.io/docs/using-k6/cookies
And if you need to pass things like Authorization headers between requests, you can extract data from one HTTP response, save it in a variable, and use it in the next requests. See https://k6.io/docs/javascript-api/k6-http/response and https://k6.io/docs/testing-guides/api-load-testing