Watch the supplementary video below to learn more about browser testing with k6 or continue reading to understand what k6 browser is, how to write and run your first test, and a look at the k6 browser API.
Over the years, k6 has become known as a performance testing tool that provides the best developer experience. Most of our efforts have focused on providing a tool that helps test your servers or backend systems. Our comprehensive load testing guide recommends keeping users in mind, and backend performance testing only addresses half of your performance testing efforts.
Suppose you test the user experience of your website and verify that there are no performance issues on a specific user journey. In that case, you need to drive some of your performance testing efforts from a browser perspective and consider a more realistic end-to-end test of the user flow.
Most load-testing tools focus on testing API endpoints, but it's different from what your users normally interact with. Your users interact with the browser, so it's also vital to test the browser's performance to get an end-to-end perspective of what's happening when interacting with your web applications.
Both frontend and backend performance testing has its pros and cons when done in isolation, which we discussed in more detail as part of the video below.
Here at k6, we want to start expanding our performance testing use case and also test beyond the protocol level.
This is where the k6 browser module comes in.
The browser module brings browser automation and end-to-end web testing to k6 while supporting core k6 features. It enables you to get insights from your front-end application during performance testing.
You can now mix browser-level and protocol-level tests in a single and unified script using k6. This can simulate the bulk of your traffic from your protocol-level tests and run one or two virtual users on a browser level to mimic how a user interacts with your website, thus leveraging a hybrid approach to performance testing.
The browser module offers a unique solution as you don’t have to use separate tools to test your frontend and backend systems. It also offers a simplified experience and aggregated view of performance metrics.
There are a lot of things happening in the preceding code, especially the introduction of asynchronous operations, so let’s break down what’s happening with the preceding code.
- We are importing browser from the k6/experimental/browser module.
- Next, we are using options to configure our test-run behaviour. We also set the browser scenario option's type to chromium, which is mandatory.
- To create a new page in your browser instance, we use the browser.newPage() inside our default function, an existing k6 functionality.
- We visit the page by using page.goto and pass the test application URL. page.goto is an asynchronous operation, so we need to wait for this to finish and use the await keyword.
- Once the operation completes, we use page.locator to interact with the elements we want. In the example, we are creating two locators. One for the login name and another one for the login password. We use the type method to type the name and password into the fields.
- To click the login button, we use the click method, which is an asynchronous operation. Clicking the submit button also causes page navigation which we need to wait to load, so page.waitForNavigation(), another asynchronous operation, is needed because the page won't be ready until the navigation completes.
- Since there are two asynchronous operations, we need to use Promise.all() to wait for the two promises to be resolved before continuing to avoid any race conditions.
- Next, we use the check feature from k6 to assert the text content of a specific element.
- Finally, we close the page and the browser.
To run the test, simply use the following command.
If you face any issues running the command, please check out our documentation for running the test.
You should see a similar test run as the video below:
With the browser launching, this provides a more visual experience as to what your users actually see so you can also find blind spots and catch issues related to browsers that won't be detected on a protocol-level.
When it's finished running, additional browser metrics are tracked as part of the k6 summary output. HTTP requests that were triggered by the browser test are also reported.
At the moment, the k6 API is synchronous. However, since many browser operations happen asynchronously, and in order to follow the Playwright API more closely, we are working on migrating most of the browser methods to be asynchronous. This means that while k6 browser is ready to be used, be warned that our API is still undergoing a few changes.
For more examples on how to use the k6 browser API, please check out k6 browser examples.
If you only consider web performance, this can lead to false confidence in overall application performance when the amount of traffic against an application increases.
It's still highly recommended to also test your backend systems to have a complete picture of your application’s performance, via the protocol-level.
However, there are problems associated with testing via the protocol level, such as:
- not being closer to the user experience since it’s skipping the browser,
- scripts can be lengthy to create and difficult to maintain as the application grows,
- browser performance metrics are ignored.
On the other hand, if you perform load testing by spinning up a lot of browsers, this requires significantly more load-generation resources, which can end up quite costly.
To address the shortcomings of each approach, a recommended practice is to adopt a hybrid approach to performance testing, which is a combination of testing both the backend and frontend systems via protocol and browser level. With a hybrid approach, you spin up the majority of your load via the protocol level, then simultaneously have one or two browser-level virtual users, so you can also have a view of what’s happening on the front end.
The great thing with k6 browser module is that it can offer you this hybrid approach to performance testing. While you can do this with multiple tools, the beauty of using this module is that it's built on top of k6, which means that you can have a protocol-level and a browser-level test in the same script.
Let's see how that translates to code.
A common scenario that we recommend is to mix a smaller subset of browser-level tests with a larger protocol-level test. To run a browser-level and protocol-level test concurrently, you can use scenarios.
Let's break down the preceeding code again.
- We are using options to configure our test-run behaviour. In this particular script, we are declaring two scenarios to configure specific workload, one for the browser-level test called browser and one for the protocol-level test called news.
- Both the browser and news scenario are using the constant-vu executor which introduces a constant number of virtual users to execute as many iterations as possible for a specified amount of time.
- Since we are using scenarios, the two functions are independent from each other and therefore, runs in parallel.
Using the same k6 run command as above, you should see a similar test output as below:
Since it's all in one script, this allows for greater collaboration amongst teams and a unified view of the performance metrics from a browser-level and protocol-level perspective.
While k6 browser started off as an extension, as of k6 version 0.43.0, it is now bundled in k6 as an experimental module, and usable without a separate binary or compilation step! We also have further plans to integrate k6 browser in k6 cloud as part of a private beta. We consider browser automation an important part of web application testing, and we have big goals for k6 browser. Our roadmap details essential status updates and our short, mid, and long-term goals.
With that said, we need your help! Since k6 browser is still relatively new, we need help from the community to try out the tool and give us feedback.