on(event, handler)
Registers a handler to be called whenever the specified event occurs. This method can also be used to prevent the too many time series
error when using the k6 browser module. For more details, refer to
Prevent too many time series error.
Events
Console event example
Metric event example
Request and response events example
import { browser } from 'k6/browser';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
// registers a handler that logs all requests made by the page
page.on('request', async (request) => console.log(request.url()));
// registers a handler that logs all responses received by the page
page.on('response', async (response) => console.log(response.url()));
await page.goto('https://quickpizza.grafana.com/', { waitUntil: 'networkidle' });
await page.close();
}
Output:
INFO[0000] https://quickpizza.grafana.com/ source=console
INFO[0001] https://quickpizza.grafana.com/api/tools source=console
INFO[0001] https://quickpizza.grafana.com/images/pizza.png source=console
...