-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
- Loading branch information
Showing
2 changed files
with
57 additions
and
22 deletions.
There are no files selected for viewing
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
77 changes: 56 additions & 21 deletions
77
...rces/next/javascript-api/k6-experimental/browser/browsercontext/waitforevent.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 |
---|---|---|
@@ -1,35 +1,70 @@ | ||
--- | ||
title: 'waitForEvent(event[, optionsOrPredicate])' | ||
excerpt: 'Waits for event to fire and passes its value into the predicate function.' | ||
excerpt: 'Waits for event to fire and returns its value.' | ||
--- | ||
|
||
# waitForEvent(event[, optionsOrPredicate]) | ||
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. | ||
|
||
{{% admonition type="caution" %}} | ||
<TableWithNestedRows> | ||
|
||
This method is a work in progress. | ||
It requires async functionality and returning a `Promise` to be useful in scripts. | ||
Refer to <a href="https://github.com/grafana/xk6-browser/issues/447">#447</a> for details. | ||
| Parameter | Type | Default | Description | | ||
|------------------------------|------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| event | string | `null` | Name of event to wait for. Currently the `'page'` event is the only one that is supported. | | ||
| 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. | | ||
| 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. | | ||
| optionsOrPredicate.timeout | number | `30000` | Optional. Maximum time to wait in milliseconds. | | ||
|
||
Consider using the sync methods `Page.waitForNavigation()` and `Page.waitForSelector()` instead. | ||
</TableWithNestedRows> | ||
|
||
{{% /admonition %}} | ||
### Returns | ||
|
||
Waits for the event to fire and passes its value into the predicate function. Returns the event data value when the predicate returns `true`. | ||
| Type | Description | | ||
|--------------------|-------------------------------------------------------------------------------------------------------| | ||
| `Promise<Object>` | At the moment a [Page](/javascript-api/k6-experimental/browser/page/) object is the only return value | | ||
|
||
<TableWithNestedRows> | ||
### Example | ||
|
||
| Parameter | Type | Default | Description | | ||
| ---------------------------- | ---------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| 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. | | ||
| 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. | | ||
| 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. | | ||
| optionsOrPredicate.timeout | number | `30000` | Maximum time to wait in milliseconds. Pass `0` to disable timeout. | | ||
<CodeGroup labels={[]}> | ||
|
||
</TableWithNestedRows> | ||
```javascript | ||
import { browser } from 'k6/x/browser'; | ||
|
||
### Returns | ||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
export default async function() { | ||
const context = browser.newContext() | ||
|
||
// Call waitForEvent with a predicate which will return true once at least | ||
// one page has been created. | ||
let counter = 0 | ||
const promise = context.waitForEvent("page", { predicate: page => { | ||
if (++counter >= 1) { | ||
return true | ||
} | ||
return false | ||
} }) | ||
|
||
// Now we create a page. | ||
const page = context.newPage() | ||
|
||
// Wait for the predicate to pass. | ||
await promise | ||
console.log('predicate passed') | ||
|
||
page.close() | ||
}; | ||
|
||
``` | ||
|
||
| Type | Description | | ||
| ------ | ------------------------------------------------------------ | | ||
| object | [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) object | | ||
</CodeGroup> |