Testing Beyond Protocol Level
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. However, we acknowledge that 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.
Here at k6, we want to start expanding our performance testing use case and also test beyond the protocol level.
This is where xk6-browser comes in.
What is xk6-browser?
xk6-browser 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 mix browser-level and protocol-level tests in a single and unified script using xk6-browser. 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.
xk6-browser offers a unique solution as you don’t have to use separate tools to test your frontend and backend systems. xk6-browser also offers a simplified experience and aggregated view of performance metrics.
Get Started with xk6-browser
To get started, the first thing that you need to do is install xk6-browser. Currently, it’s being developed as a k6 extension. However, we have plans soon to add it as an experimental module in k6 core.
Writing the test
Once you have installed xk6-browser, you can copy one of our example scripts to get started as part of our xk6-browser documentation.
Let’s break down what’s happening with the preceding code.
- We are importing chromium from the k6/x/browser module. chromium is of type BrowserType, which is xk6-browser’s entry point into launching a browser process.
- Next, we use the scenario function, an existing k6 functionality, to define our VU (virtual user) code.
- To create a new browser instance, we use the launch method of chromium, which returns a Browser object. You can pass different parameters within launch and one of the parameters you can pass is headless, which you can use to show the browser or not.
- To create a new page in your browser instance, we use the browser.newPage().
Now, on to the fun part! Let’s simulate a user visiting a test application and logging in.
There are a lot of things happening in the preceding code especially the introduction of asynchronous operations so let’s break it down again.
- We visit the page by using page.goto and pass the test application URL. We are also waiting for the network to be idle, which will succeed if there are no network connections for at least 500 ms. page.goto is also an asynchronous operation, so this returns a promise.
- Once the promise has been resolved, 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.
Running the Test
To run the test, open your terminal of choice, navigate to the directory where you have the xk6-browser binary installed, and 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, apart from the metrics that k6 already tracks, additional browser metrics are tracked as part of the k6 summary output.
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 xk6-browser methods to be asynchronous. This means that while xk6-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 xk6-browser API, please check out xk6-browser examples.
A Hybrid Approach to Performance Testing
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 xk6-browser is that it can offer you this hybrid approach to performance testing. While you can do this with multiple tools, the beauty of using xk6-browser 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.
Writing the test
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.
Running the test
Using the same xk6-browser 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.
We aim for xk6-browser to become part of the k6 core once it reaches its stability goals. We consider browser automation an important part of web application testing, and we have big goals for xk6-browser. Our roadmap details essential status updates and our short, mid, and long-term goals.
We need your help! Since xk6-browser is still relatively new, we need help from the community to try out the tool and give us feedback.