k6 Browser 23 February 2023

k6 browser module

Marie Cruz

As of k6 version 0.43.0, xk6-browser is now bundled in k6 as an experimental module! 🙌🏼

Since announcing xk6-browser in November 2021 at Grafana ObservabilityCON, the team have been busy adding on new features to include browser automation in k6. The interest from the community has also increased, which shows that browser automation and frontend performance is as important as backend performance.

Read on to learn why the team decided to merge xk6-browser into k6 core, and what code changes you need to make in your browser test scripts.

Why merge xk6-browser into k6 core?

xk6-browser started off as an extension so our users can extend k6 to support their specific testing needs. Browser automation and frontend performance is an area that we have invested in because we understand that this is also important to our users when it comes to the overall performance testing strategy. Starting it as an extension provides us with the ability to run multiple test types in k6 and differentiates us from other performance testing tools that are available in the market.

However, this also highlighted a few issues:

  • xk6-browser was detached from the core k6 product and it's distributed separately.
  • Users would have to track separate projects and build a custom binary of k6 in order to perform browser-level tests.
  • The command to run browser tests was different from the normal k6 run command.
  • A perception that we are building yet again "another performance testing tool".

By merging xk6-browser into k6 core, this will allow our users to run browser-level tests natively as part of their existing k6 setup and scripts. This allows for further simplicity, ease of maintenance, and closer to providing a hybrid approach to performance testing.

Going forward, xk6-browser will now be referred to as k6 browser or simply the browser module now that it's bundled in k6 natively.

Migration plan for your browser tests

If you have already been using xk6-browser version 0.8.0, there is minimal configuration required as the scripts should continue to work with the built-in browser module in k6 v0.43.0. To use your existing scripts, simply change the import path from import { chromium } from 'k6/x/browser' to import { chromium } from 'k6/experimental/browser'.

This version of k6 now has native support for JavaScript's async/await syntax. So any asynchronous operations such as page.goto() can now use async/await rather than the promisified .then().

Here's an example script:

script.js
1import { chromium } from 'k6/experimental/browser';
2
3export default async function () {
4 const browser = chromium.launch({ headless: false });
5 const page = browser.newPage();
6 try {
7 await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' })
8 page.screenshot({ path: 'screenshot.png' });
9 } finally {
10 page.close();
11 browser.close();
12 }
13}

To run this test, you now have to use the following command:

CLI
$ K6_BROWSER_ENABLED=true k6 run script.js

For security considerations, the environment variable K6_BROWSER_ENABLED has been introduced to only allow the usage of browsers after it's been explicitly allowed. This requirement is temporary and may be removed in a future k6 release.

What's next?

Our next step is to drop the experimental label and make browser support a stable feature of k6. We also have plans of eventually enabling it in k6 Cloud.

If you find any issues, please raise them as part of our k6 Github project or ask away in our community forum for additional support.

Read more

To know more about new changes, check out our up-to-date get started guide as well as our official documentation to understand all the changes introduced.

< Back to all posts