Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/browser/page/on.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/browser/page/on/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
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.
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
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

