Skip to content

Commit

Permalink
Add k6 browser page.on method docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ka3de committed Aug 31, 2023
1 parent 5a55e66 commit fc93351
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: 'on(event, handler)'
excerpt: 'Browser module: page.on method'
---

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. |


<Blockquote mod="attention" title="">

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.

</Blockquote>


### 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`](/javascript-api/k6-experimental/browser/consolemessage) class. |


### Example

<CodeGroup labels={[]}>

```javascript
import { browser } from 'k6/x/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
}
}
```

</CodeGroup>

0 comments on commit fc93351

Please sign in to comment.