Using browser in the setup stage of a test

With the new k6 version 0.46 it is necessary to define the browser type in the scenario. For the real running test that is fine and makes it easy to configure the browser. But I have to call a browser during the setup stage once. But how can I configure the browser type in the setup stage?

1 Like

Hi @mraab , welcome to the forum! :wave:

Could you give us more details on what are you trying to achieve and your exact use case for this please?

Thank you.

In our setup stage we have to configure the server. This has to be done over a frontend in a browser, because no REST interface is available. Therefore we start a browser, login, select a special page and check a configuration. If the configuration is wrong we change this in the stage.

This has to run once in stage phase and currently we do this in this way:

export function setup() {
  cache.pageCache(cacheEnabled)
}

cache.pageCache is method in a module:

export async function pageCache(cache_should_be_enabled: boolean) {
  const browser = chromium.launch({
    headless: true , args: ['no-sandbox']
  })
....

But now with version 0.46 this was changed.

Thank you for the explanation @mraab

Unfortunately, as of now, the browser module can only be used in the iteration body.
From your explanation, it looks like your setup can be done completely away from the test implementation, as it does not pass any data to the iteration functions etc. If that is not an option, a workaround would be to use two different scenarios, one that would run the UI scenario, making sure that the server is configured properly, and one that would actually run the load test. See this post on how to run scenarios sequentially.

Let me know if that helps.

2 Likes

Thank you. That is working fine for us.