How to get authenticated browser instance before test

Hi @vkovalev :wave:

You won’t be able to pass a reference to the browser instance initialized in setup to the VUs function.

If you want to avoid having to launch a browser instance every time, currently the best option is to use the BrowserType.Connect() method. You’ll have to handle the browser lifecycle independently, but the browser instance will not be initialized in each iteration, instead k6 browser would connect to it.

Unfortunately right now it’s not possible to inherit the browser instance session/user data, as k6 browser will create a new browserContext on every new page created. This is something that we plan to work on in the short term.
I think the best option is to perform the login in the setup phase, retrieve the session cookie by using JS code and page.evaluate() mehtod (as currently it’s not supported to retrieve HTTP only cookies :slightly_frowning_face: , see here) and pass the session cookie to the VUs body function where you can reuse it in every browser context by using the addCookies() method.

If the cookie is HTTP only, another option would be to login and retrieve the cookie through the HTTP client instead, or through other means that you can automate.

I reckon there are quite a few pain points in this process that we are actively trying to improve in the short term. Stay tuned for next releases :bowing_man:

1 Like