This guide outlines the key changes you will need to make when moving your existing k6 browser test scripts to the new k6 browser module (bundled with k6 version 0.46).
Key changes
The updated version introduces notable structural changes in its operation and API, including breaking changes:
- The import path for the browser module has switched from chromium to browser.
- Simplified resource management. The browser module now handles the startup and shutdown of browser processes automatically. The chromium.launch(), chromium.connect(), and browser.close() methods are no longer necessary and have been removed.
- Browser options can now only be set using environment variables.
- Scenario options must now be defined for running browser tests.
- Single browser context per iteration. You can now only run a single BrowserContext at a time in the same iteration.
noteYou no longer need to use the K6_BROWSER_ENABLED flag when running browser tests with the k6 command.
Before and after comparison
Let's start with an overview of the main differences between the previous and new versions of the k6 browser API.
Import path
With the browser type (specifically chromium) now set in scenario options, you should directly import the browser object from the browser module.
Simplified resource management
The browser lifecycle is now automatically managed by the browser module, and so the chromium.launch(), chromium.connect(), and browser.close() methods are no longer necessary and have been removed.
Now, all that is needed is to specify the browser.type within the scenario options. If the option is set, a browser instance will automatically start at the beginning and close at the end of each test iteration.
Browser options
With the removal of the chromium.launch() and chromium.connect() methods, setting browsers options is now done by using environment variables. For more information, refer to Browser Module Options.
Before:
After:
noteThe following browser options are no longer supported: devtools, env, and proxy since they weren't providing much value. slowMo has been temporarily removed, and we're working on reintroducing it.
Scenario options
You must now set the executor and browser type as options in a scenario definition. Specifically, the browser.type option should be set to chromium, as Chromium is the only browser supported.
Opening and closing a page
You can open a new page by using the imported browser object's browser.newPage() method. You can still use the Page object as before.
The browser.close() method has been removed, so you can remove that from your scripts and use page.close() once you're done using the page object.
noteClosing of the page is critical for the calculation of accurate Web Vital metrics. See the browser metrics for more details.
Browser context limit
The new browser implementation limits the usage to a single active BrowserContext per iteration. This change enhances the prediction of resource requirements for a test run and promotes the use of scenarios to separate independent browser sessions.
- A new BrowserContext can be created either with the browser.newContext() or browser.newPage() methods.
- If a new BrowserContext needs to be created, the existing one must be closed first using the browserContext.close() method.
- Alongside these changes, the browser.contexts() method has been altered to browser.context() to retrieve the current BrowserContext.
For instance, the code below will not function as intended, as it attempts to execute two simultaneous BrowserContexts within the same iteration.
On the other hand, the next example will function correctly by closing the initial BrowserContext prior to establishing a new one.
These updates make the usage of the API more straightforward for users, aiding in more consistent and automatic resource management.
For all the details, make sure to review the complete changelog for k6 version 0.46. For more information watch k6 Office Hours #98, where we discuss the latest changes in k6 browser, and, as always, ask in the community forum if you need help!