BrowserContext.storageState(opts) has not been implemented yet. Any workarounds?

Hi there,

I am using k6 experimental module browser, and experiencing this Go error: BrowserContext.storageState(opts) has not been implemented yet

This is probably because k6 browser doesn’t support .storageState yet. Do you know any workarounds?

My goal is to automate logging process so that the tests don’t need to sing in every time, but instead have logged in state: inspired by Playwright’s auth process

When I follow the script from PW’s docs, I’m getting Go error (the above error). Probably, k6 not supporting this feature yet? Looking for any workarounds. Thanks!

My script:

// WP Admin auth
export async function auth(page) {
  const authFile = '../admin.json';
  await page.goto(`${baseURL}/wp-admin`, {
    waitUntil: 'networkidle',
  });
  await page.waitForLoadState('networkidle');
  await page.locator('input[name="log"]').type('admin');
  await page.locator('input[name="pwd"]').type('password');
  await page.locator('input[name="wp-submit"]').click();
  await page.waitForSelector('h1');
  await page.waitForLoadState('networkidle');
  await page.context().storageState({ authFile });
}

I am actually getting the same error even by using addCookies from the following k6 tutorial: addCookies()

Might be that I have a different problem here. Investigating it now.

Hi @moonbridge,

storageState is an unimplemented method. addCookies on the other hand should work. Which version of k6 are you running? v0.44.1 is the latest version of k6.

Cheers,
Ankur

1 Like

Hey @ankur
Thanks for the quick feedback. I was running 0.43.0, now with 0.44.1 I don’t see the issue anymore!

Yes, with addCookies it is working. What I did is manually logged in, extracted name and value from the logged in user cookie… and pasted in the code like:

const browser = chromium.launch({
    headless: true,
    timeout: timeoutSet,
  });
  const context = browser.newContext();
  const page = context.newPage();
  context.addCookies([
    {
      name: '*******',
      value: '********',
      url: `${baseURL}`,
    },
  ]);

  // Go to the page
  await page.goto(
    `${baseURL}/wp-admin/`,
    {
      waitUntil: 'networkidle',
    },
  );

Now I don’t see logging anymore, automatically logged in for every test case.

1 Like

Hi @ankur
Since storageState is not implemented, what is the alternative to store cookie and use to log in automatically for every next test case?

Probably I need to try something with BrowserContext from Playwright, especially browserContext.cookies().

Many thanks

Hi @moonbridge,

At the moment there is no way to write and read cookies from disk that can be accessed by different vus and iterations with the browser module. You could try writing to a distributed storage system, and read from it – the details of which can be found in this thread.

Hope that helps,
Ankur