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 28, 2023
1 parent 966657e commit b19c731
Showing 1 changed file with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: 'on(event, handler)'
excerpt: 'Browser module: page.on method'
---

Allows the user to define an event handler function that will be executed every time the specified event is emitted.

| Parameter | Type | Default | Description |
|-----------------|--------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| event | string | `''` | Event to attach handler to. Currently, only `'console'` event is supported. |
| handler | function | `null` | A function to be called every time the specified event is emitted. |


<Blockquote mod="warning" title="">

When using `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>


### on('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 b19c731

Please sign in to comment.