Menu
Scroll for more
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Open source
waitForEvent(event[, optionsOrPredicate])
Waits for the specified event to be emitted. This is a generic method that can wait for any page event such as console, request, or response.
| Parameter | Type | Default | Description |
|---|---|---|---|
| event | string | - | Required. Event name. Supported events: console, request, response. |
| optionsOrPredicate | function | object | null | Either a predicate function or an options object. If a function, it will be used as the predicate. |
| options.predicate | function | null | A function that returns true when the expected event is received. The event object is passed as argument. |
| options.timeout | number | 30000 | Maximum time in milliseconds. Pass 0 to disable the timeout. Default is overridden by the setDefaultTimeout option on
BrowserContext or
Page. |
Returns
| Type | Description |
|---|---|
| Promise | A Promise that fulfills with the event data when the event is emitted. The return type depends on the event:
ConsoleMessage for console,
Request for request,
Response for response. |
Examples
Wait for console message
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();
try {
// Set up the wait before triggering the action
const consolePromise = page.waitForEvent('console');
// Trigger action that causes a console message
await page.evaluate(() => console.log('hello from page'));
const msg = await consolePromise;
console.log(`Console message: ${msg.text()}`);
} finally {
await page.close();
}
}Wait for response with predicate
JavaScript
import { browser } from 'k6/browser';
import { check } from 'k6';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://quickpizza.grafana.com/');
// Wait for a specific response using a predicate function
const responsePromise = page.waitForEvent('response', (res) =>
res.url().includes('/api/pizza')
);
await page.getByRole('button', { name: /pizza/i }).click();
const response = await responsePromise;
check(response, {
'response status is 200': (r) => r.status() === 200,
});
} finally {
await page.close();
}
}Best practices
- Set up promise before trigger: Always set up the
waitForEventpromise before triggering the action that causes the event:
JavaScript
// Correct
const eventPromise = page.waitForEvent('console');
await page.evaluate(() => console.log('test'));
const msg = await eventPromise;
// Incorrect - may miss the event
await page.evaluate(() => console.log('test'));
const msg = await page.waitForEvent('console');- Use specific methods when available: For common use cases, prefer the more specific methods like
waitForRequestorwaitForResponsewhich provide URL pattern matching.
Related
- page.on() - Subscribe to page events
- page.waitForRequest() - Wait for HTTP requests with URL pattern matching
- page.waitForResponse() - Wait for HTTP responses with URL pattern matching
Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.