Skip to content

Commit

Permalink
Add a new example on waitForEvent
Browse files Browse the repository at this point in the history
The new example will work with a new tab to wait for the new page to be
created before continuing on with the test.

It also works with ControlOrMeta.
  • Loading branch information
ankur22 committed Oct 4, 2024
1 parent 9d9cc73 commit 1aac61a
Showing 1 changed file with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,65 @@ Waits for the event to fire and returns its value. If a predicate function has b

### Returns

| Type | Description |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Type | Description |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<Page>` | A Promise that fulfills with a [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page) object when the `page` event has been emitted. |

### Example

This API is useful when you want to wait for the new page to open after clicking on a link that opens in a new tab.

<CodeGroup labels={[]}>

```javascript
import { browser } from 'k6/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

await page.goto('https://test.k6.io/');

await page.keyboard.down('ControlOrMeta');

// Open the link in a new tab with the help of the meta key.
// Wait for the new page to be created.
const browserContext = browser.context();
const [newTab] = await Promise.all([
browserContext.waitForEvent('page'),
await page.locator('a[href="/my_messages.php"]').click(),
]);

await page.keyboard.up('ControlOrMeta');

// Wait for the new page (tab) to load.
await newTab.waitForLoadState('load');

// Take screenshots of each page.
await page.screenshot({ path: `screenshot-page.png` });
await newTab.screenshot({ path: `screenshot-newTab.png` });

await newTab.close();
await page.close();
}
```

</CodeGroup>

Here's an example working with the predicate:

<CodeGroup labels={[]}>

```javascript
Expand Down

0 comments on commit 1aac61a

Please sign in to comment.