Can not pass data from setup to default function in XK6

Hello,
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;
  page
    .goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' })
    .then(() => {
      // Enter login credentials and login
      page.locator('input[name="login"]').type('admin');
      page.locator('input[name="password"]').type('123');

      // Wait for asynchronous operations to complete
      return Promise.all([
        page.waitForNavigation(),
        page.locator('input[type="submit"]').click(),
      ]).then(() => {
        check(page, {
          'header': page.locator('h2').textContent() == 'Welcome, admin!',
        });
        pageTitle = page.title();
      });
    }).finally(() => {
      page.close();
      browser.close();
      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