grafana/k6 · error
waiting for page event
Error message
waiting for page %s event: %w
What it means
Page.waitForEvent wraps every failure of an event wait in 'waiting for page <event> event', including the event name and the underlying cause. waitForEvent subscribes to a page event (e.g. 'popup', 'load', 'domcontentloaded', 'console') and resolves when a predicate accepts it; the deferred wrap catches timeout expirations, context cancellation, and predicate errors alike.
Solutions
- Read the wrapped cause after the event name — 'context deadline exceeded' means timeout; raise the timeout option.
- For 'networkidle' waits on pages with long-lived connections, switch to waitUntil: 'load' or 'domcontentloaded'.
- Ensure the action that should emit the event (click, submit) actually ran before/while waiting — combine with page.waitForEvent before the triggering action using Promise.all.
- Verify popups are not blocked and the link really uses target=_blank or window.open.
- If the whole test was aborted, look at k6 iteration/time budgets instead of the wait itself.
Example fix
// before
const p = page.waitForEvent('popup', { timeout: 1000 });
await page.click('#opens-popup'); // popup wait may time out first
// after
const [popup] = await Promise.all([
page.waitForEvent('popup', { timeout: 10_000 }),
page.click('#opens-popup'),
]); Defensive patterns
Strategy: try-catch
Validate before calling
// pair the wait with the action that triggers the event so it cannot time out spuriously // (see tryCatchPattern)
Try / catch
try {
const [popup] = await Promise.all([
page.waitForEvent('popup', { timeout: 10_000 }),
page.click('#open-window'),
]);
} catch (e) {
if (/waiting for page popup event.*deadline/i.test(String(e))) { /* popup never opened */ }
else throw e;
} Prevention
- Always start the waitForEvent promise before (or with Promise.all around) the triggering action.
- Prefer 'load'/'domcontentloaded' over 'networkidle' on pages with persistent sockets.
- Set explicit generous timeouts on slow CI.
When it happens
Trigger: await page.waitForEvent('popup', { timeout: N }) where no matching popup/open fires within the timeout; page.waitForLoadState() waiting for a lifecycle event that never fires; the k6 test context being cancelled (test iteration ending) while the wait is pending; or the predicate itself returning an error.
Common situations: Waiting for a popup that a click only triggers under certain conditions (popup blockers, target=_blank links removed); waiting for 'networkidle' on pages with persistent connections (websockets, analytics heartbeats) so it never settles; timeouts set too low on slow CI environments.
Related errors
- waitForEvent timed out after
- an error occurred communicating with k6 Cloud
- " " attribute is only supported for roles: "`).join(", ")}
- " " does not support " " matcher
- " " must be one of
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/cd2127ba7917131d.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/page.go:1963
for _, next := range handlers {
if !yield(next.handler) {
return
}
}
}
}
// waitForEvent subscribes to the given event and resolves once the predicate
// determines an event should complete the wait or terminate with an error.
func (p *Page) waitForEvent(
ctx context.Context,
eventName PageEventName,
predicate func(PageEvent) (bool, error),
) (_ PageEvent, rerr error) {
defer func() {
if rerr != nil {
rerr = fmt.Errorf("waiting for page %s event: %w", eventName, rerr)
}
}()
type pageEventWaitResult struct {
event PageEvent
err error
}
var (
once sync.Once
result = make(chan pageEventWaitResult, 1)
)
id, err := p.addEventHandler(eventName, func(event PageEvent) error {
ok, perr := predicate(event)
if perr == nil && !ok {
return nil
}
// We don't want to deadlock if another event frequently happens.View on GitHub (pinned to 3fcf5388d7)