This post was originally written by Yusuf Tayman in dev.to.
In this article, we will discover the process of sending custom metrics, provided to us through the xk6-browser, in order to gain a better understanding of our performance tests. Through the integration of these metrics, we will be able to create more tailored Slack messages that will help to manage our rule sets. Furthermore, this process will give us a more detailed insight into our performance tests, with the custom metrics helping to provide granular data about our performance tests and the associated rule sets. This will allow us to identify any issues that may arise, and take the necessary action to ensure that our performance tests are running as efficiently as possible.
xk6-browser provides us with the power of browser automation and end-to-end web testing, all while supporting core k6 features. It adds a set of APIs at the browser-level to interact with browsers, enabling developers to collect frontend performance metrics as part of their k6 tests. This is a powerful tool for anyone looking to develop web applications with an emphasis on performance and reliability.
The metrics are still subject to change as k6 tries to align them to the standard web vitals.
On the other hand, xk6-browser was designed to provide rough compatibility with the Playwright API, so developers don't need to learn an entirely new API to take advantage of the browser automation and web testing capabilities that xk6-browser provides. This means that developers can easily transition to using xk6-browser, taking advantage of the powerful features it has to offer.
In the latest development of k6, you don't need to build xk6-browser. It has already been included in the k6 package. You need to upgrade k6 to v.43.0 version to implement it like the k6 package.
And also you have to set env var K6_BROWSER_ENABLED=true that runs browser tests.
Experimental module, use at your own risk
There are still changes, please follow the releases regularly.
Let's create a simple example with xk6-browser. This script navigates to the login page, enters login credentials, submits the form and checks if the login was successful. It also closes the browser and page after the test was completed.
One of the essential features of k6 is that we can get all the metrics we want. You can get it as stdout, XML, or JSON if you want. For this, we need to use the handleSummary() function. You can also use this function with xk6-browser. It provides us with all the metrics in JSON form after the test. Since we have these metrics, we can use them however we want. Especially if you are measuring the web performance of your site and want to follow it on slack, xk6-browser provides us with the metrics we want.
You will see the output below, it provides us with all the values as stdout.
You can create many more custom metrics for handleSummary, remove what you want and add any metrics you want. For all this, you can check the document where handleSummary is explained in detail.
We know how to custom-pull our metrics. Now we can create a custom slack message.
To customize Slack messages, I use the Block Kit Builder page offered by slack, where you can customize your messages as you wish. Other than that, all we need is a slack bot token. You can get bot tokens from an existing app or by creating a new app. You can examine this page in detail.
You can follow the steps below to create the function to that we will send the Slack message;
- First of all, we determine the metrics we will pull and access them from the data object (handleSummary provides us with this data object).
- Then we create our custom message and add the metrics we have taken into our message.
- Finally, we send our message to the channel we want with the slack chat.postMessage method.
Now that we've created our function, let's see how to use it in handleSummary.
We are now ready to run our test.
Let's check slack, yes your message has dropped on our channel, we can see the message that we shot and added as a custom. Also, since it is approved by the crocodile, we can use it easily. :)
From here on, it depends on our imagination and requirements. You can edit which metrics you want to use, and you can even create an alert mechanism according to the rules of Core Web Vitals.