Menu
Choose a product
Viewing: v0.48.x
Find another version
Scroll for more
Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
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, only the 'console' event is supported. |
| handler | function | null | A function to be called every time the specified event is emitted. |
Caution
When using the
page.onmethod, the page has to be explicitly closed for the iteration to be able to finish.
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. |
Example
JavaScript
JavaScript
import { browser } from 'k6/experimental/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 = 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,
});
});
page.evaluate(() => console.log('this is a console.log message', 42));
} finally {
page.close(); // required so iteration can end
}
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

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.