|
| 1 | +--- |
| 2 | +title: 'on(event, handler)' |
| 3 | +excerpt: 'Browser module: page.on method' |
| 4 | +--- |
| 5 | + |
| 6 | +Registers a handler to be called whenever the specified event occurs. |
| 7 | + |
| 8 | +| Parameter | Type | Default | Description | |
| 9 | +|-----------------|--------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 10 | +| event | string | `''` | Event to attach the handler to. Currently, only the `'console'` event is supported. | |
| 11 | +| handler | function | `null` | A function to be called every time the specified event is emitted. | |
| 12 | + |
| 13 | + |
| 14 | +<Blockquote mod="attention" title=""> |
| 15 | + |
| 16 | +When using the `page.on` method, the page has to be explicitly [closed](/javascript-api/k6-experimental/browser/page/close/) for the iteration to be able to finish. |
| 17 | + |
| 18 | +</Blockquote> |
| 19 | + |
| 20 | + |
| 21 | +### Events |
| 22 | + |
| 23 | +| Event | Description | |
| 24 | +|-----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 25 | +| `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`](/javascript-api/k6-experimental/browser/consolemessage) class. | |
| 26 | + |
| 27 | + |
| 28 | +### Example |
| 29 | + |
| 30 | +<CodeGroup labels={[]}> |
| 31 | + |
| 32 | +```javascript |
| 33 | +import { browser } from 'k6/x/browser'; |
| 34 | +import { check } from 'k6'; |
| 35 | + |
| 36 | +export const options = { |
| 37 | + scenarios: { |
| 38 | + ui: { |
| 39 | + executor: 'shared-iterations', |
| 40 | + options: { |
| 41 | + browser: { |
| 42 | + type: 'chromium', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + thresholds: { |
| 48 | + checks: ["rate==1.0"] |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export default async function() { |
| 53 | + const page = browser.newPage(); |
| 54 | + |
| 55 | + try { |
| 56 | + await page.goto('https://test.k6.io/'); |
| 57 | + |
| 58 | + page.on('console', msg => { |
| 59 | + check(msg, { |
| 60 | + 'assertConsoleMessageType': msg => msg.type() == 'log', |
| 61 | + 'assertConsoleMessageText': msg => msg.text() == 'this is a console.log message 42', |
| 62 | + 'assertConsoleMessageArgs0': msg => msg.args()[0].jsonValue() == 'this is a console.log message', |
| 63 | + 'assertConsoleMessageArgs1': msg => msg.args()[1].jsonValue() == 42, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + page.evaluate(() => console.log('this is a console.log message', 42)); |
| 68 | + } finally { |
| 69 | + page.close(); // required so iteration can end |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +</CodeGroup> |
0 commit comments