Skip to content

Commit 5aaa6ab

Browse files
committed
Update browserContext.waitForEvent
- It removes the WIP tags and reformats the table markdown on the main browserContext page. - It removes the WIP notice, updates the description, updates the return value, and adds an example in the waitForEvent page.
1 parent 46f3da6 commit 5aaa6ab

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

docs/sources/next/javascript-api/k6-experimental/browser/browsercontext/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ If a [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimen
2727
| [BrowserContext.setDefaultTimeout(timeout)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. |
2828
| [BrowserContext.setGeolocation(geolocation)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/setgeolocation) <BWIPT id="435"/> | Sets the `BrowserContext`'s geolocation. |
2929
| [BrowserContext.setOffline(offline)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. |
30-
| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/waitforevent) <BWIPT id="447"/> | Waits for the event to fire and passes its value into the predicate function. |
30+
| [BrowserContext.waitForEvent(event[, optionsOrPredicate])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. |
Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,70 @@
11
---
22
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.'
44
---
55

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

8-
{{% admonition type="caution" %}}
8+
<TableWithNestedRows>
99

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

14-
Consider using the sync methods `Page.waitForNavigation()` and `Page.waitForSelector()` instead.
17+
</TableWithNestedRows>
1518

16-
{{% /admonition %}}
19+
### Returns
1720

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

20-
<TableWithNestedRows>
25+
### Example
2126

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={[]}>
2828

29-
</TableWithNestedRows>
29+
```javascript
30+
import { browser } from 'k6/x/browser';
3031

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+
```
3269

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

Comments
 (0)