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.
You no longer need to use the K6_BROWSER_ENABLED flag when running browser tests with the k6 command.
Let's start with an overview of the main differences between the previous and new versions of the k6 browser API.
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.
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.
The 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.
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.
Closing of the page is critical for the calculation of accurate Web Vital metrics. See the browser metrics for more details.
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!