Share values with other runs

Hi all,
I am wanting to use values from a previous scenario into the next. is that possible?


Potentially yes, depending on what values you want to transfer from scenario to scenario, but it’s probably not going to be easy… :disappointed: Can you elaborate a bit more on exactly what you want to do? Since k6 scripts can’t write to external files, you’d have to use some intermediary web services or something like that to store the values you’re interested in.

Im wanting to use the same sessionIds from a previous “login” scenario instead of rerunning login before every scenario. Is that doable? - if so, any docs that can help me :slight_smile: thanks!

Hmm there might be a slight misunderstanding here - what do you mean by “scenario”? Because if you mean each iteration of the default function, then you can very easily do a single login in setup() and pass the data to the default function to reuse. Take a look at the test life-cycle description in the k6 docs: Test lifecycle

ahh sorry about that! What I meant by scenarios is from a different run. ie.

k6 run scenario1.js
k6 run scenario2.js

Where i want to grab the session ids that was returned from scenario1.js into scenario2.js

Is that possible, or will i have to set up a setup() to relogin in scenario2.js?


While it sure is possible to share data between test runs using external services and such, it really isn’t something I would recommend as it will be very hard to 1) keep track of, 2) maintain and 3) get right.

I’d recommend that you just login in the setup function of each scenario. If you want to have less code duplication, you could extract this login code to its own helper file, for instance:

// login.helper.js

export function login(username, password) {
 // your login logic that returns a session token
// scenario.js
import { login } from './login.helper.js';

export function setup() {
  const token = login(username, password);
  return { token };

export default function(data) {
  // access token through data.token