This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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
import { browser } from 'k6/browser';
import { check } from 'k6';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://test.k6.io/');
page.on('console', (msg) => {
check(msg, {
assertConsoleMessageType: (msg) => msg.type() == 'log',
assertConsoleMessageText: (msg) => msg.text() == 'this is a console.log message 42',
assertConsoleMessageArgs0: (msg) =>
msg.args()[0].jsonValue() == 'this is a console.log message',
assertConsoleMessageArgs1: (msg) => msg.args()[1].jsonValue() == 42,
});
});
await page.evaluate(() => console.log('this is a console.log message', 42));
} finally {
await page.close(); // required so iteration can end
}
}Metric event 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();
// We first register a handler using page.on('metric'). Calling page.on('metric')
// multiple times is allowed. The registered handlers will be executed in the
// order page.on was called.
page.on('metric', (metric) => {
// Using metric.tag finds a match between the current metric url and name
// tags against the supplied regular expression in `url`.
//
// At the moment metric.tag is the only method on the metricMessage object.
metric.tag({
// This is the new name value that will replace the existing value in the
// url and name tags when a match is found.
name: 'test',
// You can provide multiple matches here.
matches: [
{
url: /^https:\/\/test\.k6\.io\/\?q=[0-9a-z]+$/,
// When a method is defined it will also need to match on that too. If a
// method is not provided it will match on all method types.
method: 'GET',
},
],
});
});
try {
// This is only for illustrative purposes, the q query param doesn't affect
// the website.
await page.goto('https://test.k6.io/?q=abc123');
await page.goto('https://test.k6.io/?q=def456');
} finally {
await page.close();
}
}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
...

