Can not pass data from setup to default function in XK6

My scenario is to run the login once in the setup function then share some variable generate on it (like page.title ) to default function, but when trying to get the variable on default function I got undefined , I expected to get “My messages” text, this is my script example:

import { sleep } from 'k6';
import { chromium } from 'k6/x/browser';
import { check } from 'k6'

export function setup() {
  const browser = chromium.launch({ headless: false });
  const context = browser.newContext();
  const page = context.newPage();
  let pageTitle;
    .goto('', { waitUntil: 'networkidle' })
    .then(() => {
      // Enter login credentials and login

      // Wait for asynchronous operations to complete
      return Promise.all([
      ]).then(() => {
        check(page, {
          'header': page.locator('h2').textContent() == 'Welcome, admin!',
        pageTitle = page.title();
    }).finally(() => {
      console.log(`LAST: ${pageTitle}`);

    return pageTitle

export default function (data) {
    console.log("Default_Data:" + data);
       //I have planned to do something with data

Could you please help me to resolve this problem?

My k6 version is k6 v0.36.0 ((devel), go1.19.5, windows/amd64)

Hi, @vsanabria,

The setup function does not entirely support async. However, you can use the solution explained in this issue.

Let me know if it helps.

1 Like