|
1 | 1 | ---
|
2 | 2 | title: 'waitForEvent(event[, optionsOrPredicate])'
|
3 |
| -excerpt: 'Waits for event to fire and passes its value into the predicate function.' |
| 3 | +excerpt: 'Waits for event to fire and returns its value.' |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -# waitForEvent(event[, optionsOrPredicate]) |
| 6 | +Waits for the event to fire and returns its value. If a predicate function has been set it will pass the value to the predicate function, which must return `true` for the promise to resolve. |
7 | 7 |
|
8 |
| -{{% admonition type="caution" %}} |
| 8 | +<TableWithNestedRows> |
9 | 9 |
|
10 |
| -This method is a work in progress. |
11 |
| -It requires async functionality and returning a `Promise` to be useful in scripts. |
12 |
| -Refer to <a href="https://github.com/grafana/xk6-browser/issues/447">#447</a> for details. |
| 10 | +| Parameter | Type | Default | Description | |
| 11 | +|------------------------------|------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 12 | +| event | string | `null` | Name of event to wait for. Currently the `'page'` event is the only one that is supported. | |
| 13 | +| optionsOrPredicate | function\|object | `null` | Optional. If it's a function, the `'page'` event data will be passed to it and it must return `true` to continue. | |
| 14 | +| optionsOrPredicate.predicate | function | `null` | Optional. Function that will be called when the `'page'` event is emitted. The event data will be passed to it and it must return `true` to continue. | |
| 15 | +| optionsOrPredicate.timeout | number | `30000` | Optional. Maximum time to wait in milliseconds. | |
13 | 16 |
|
14 |
| -Consider using the sync methods `Page.waitForNavigation()` and `Page.waitForSelector()` instead. |
| 17 | +</TableWithNestedRows> |
15 | 18 |
|
16 |
| - {{% /admonition %}} |
| 19 | +### Returns |
17 | 20 |
|
18 |
| -Waits for the event to fire and passes its value into the predicate function. Returns the event data value when the predicate returns `true`. |
| 21 | +| Type | Description | |
| 22 | +|--------------------|-------------------------------------------------------------------------------------------------------| |
| 23 | +| `Promise<Object>` | At the moment a [Page](/javascript-api/k6-experimental/browser/page/) object is the only return value | |
19 | 24 |
|
20 |
| -<TableWithNestedRows> |
| 25 | +### Example |
21 | 26 |
|
22 |
| -| Parameter | Type | Default | Description | |
23 |
| -| ---------------------------- | ---------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | |
24 |
| -| event | string | `null` | Name of event to wait for. **NOTE**: Currently this argument is disregarded, and `waitForEvent` will always wait for `'close'` or `'page'` events. | |
25 |
| -| optionsOrPredicate | function\|object | `null` | Optional. If it's a function, the `'page'` event data will be passed to it and it must return `true` to continue. | |
26 |
| -| optionsOrPredicate.predicate | function | `null` | Function that will be called when the `'page'` event is emitted. The event data will be passed to it and it must return `true` to continue. | |
27 |
| -| optionsOrPredicate.timeout | number | `30000` | Maximum time to wait in milliseconds. Pass `0` to disable timeout. | |
| 27 | +<CodeGroup labels={[]}> |
28 | 28 |
|
29 |
| -</TableWithNestedRows> |
| 29 | +```javascript |
| 30 | +import { browser } from 'k6/x/browser'; |
30 | 31 |
|
31 |
| -### Returns |
| 32 | +export const options = { |
| 33 | + scenarios: { |
| 34 | + browser: { |
| 35 | + executor: 'shared-iterations', |
| 36 | + options: { |
| 37 | + browser: { |
| 38 | + type: 'chromium', |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +export default async function() { |
| 46 | + const context = browser.newContext() |
| 47 | + |
| 48 | + // Call waitForEvent with a predicate which will return true once at least |
| 49 | + // one page has been created. |
| 50 | + let counter = 0 |
| 51 | + const promise = context.waitForEvent("page", { predicate: page => { |
| 52 | + if (++counter >= 1) { |
| 53 | + return true |
| 54 | + } |
| 55 | + return false |
| 56 | + } }) |
| 57 | + |
| 58 | + // Now we create a page. |
| 59 | + const page = context.newPage() |
| 60 | + |
| 61 | + // Wait for the predicate to pass. |
| 62 | + await promise |
| 63 | + console.log('predicate passed') |
| 64 | + |
| 65 | + page.close() |
| 66 | +}; |
| 67 | + |
| 68 | +``` |
32 | 69 |
|
33 |
| -| Type | Description | |
34 |
| -| ------ | ------------------------------------------------------------ | |
35 |
| -| object | [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) object | |
| 70 | +</CodeGroup> |
0 commit comments