on(event, handler)
Open source
on(event, handler)
Registers a handler to be called whenever the specified event occurs.
Parameter | Type | Default | Description |
---|---|---|---|
event | string | '' | Event to attach the handler to. Currently, 'console' and 'metric' events are supported. |
handler | function | null | A function to be called every time the specified event is emitted. |
Events
Event | Description |
---|---|
console | Emitted every time the console API methods are called from within the page JavaScript context. The arguments passed into the handler are defined by the ConsoleMessage class. |
metric | Emitted every time a metric is measured and emitted for the page. The arguments passed into the handler are defined by the MetricMessage object. |
Console event Example
JavaScript
import { browser } from 'k6/browser';
import { check } from 'k6';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
thresholds: {
checks: ['rate==1.0'],
},
};
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
JavaScript
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();
}
}
Was this page helpful?
Related documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
60 min

Performance testing and observability in Grafana Cloud
In this webinar, learn how Grafana Cloud k6 offers you the best developer experience for performance testing.
60 min

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.