-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...ata/markdown/docs/02 javascript api/07 k6-experimental/01 browser/10 Page/on.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |