grafana/k6 · error

waitForEvent timed out after

Error message

waitForEvent timed out after %v

What it means

waitForEvent('page', ...) ran the full timeout without a new page satisfying the condition: either no EventBrowserContextPage fired in the context during the window, or pages were created but the predicate never returned true (a predicate error returns a different error). The context remains usable — only this wait failed. Context cancellation is reported separately via ContextErr.

Solutions

  1. Pass a larger explicit timeout: ctx.waitForEvent('page', null, 30000) or raise the default with ctx.setDefaultTimeout(30000)
  2. Make sure the action that opens the new page (click/submit) actually runs, and runs in the same context
  3. Fix the predicate: log the actual page.url() inside it and compare against the real URL
  4. Verify the site still opens a new tab rather than navigating in the same one

Example fix

// before
const popup = await ctx.waitForEvent('page', (p) => p.url().includes('login'), 1000);

// after
ctx.setDefaultTimeout(30000);
const popupPromise = ctx.waitForEvent('page', (p) => p.url().includes('login'), 30000);
await page.locator('#open-popup').click(); // trigger AFTER starting the wait
const popup = await popupPromise;
Defensive patterns

Strategy: try-catch

Validate before calling

// start the wait BEFORE the triggering action, with an explicit, generous timeout
const popupPromise = ctx.waitForEvent('page', (p) => p.url().includes('/oauth'), 30000);
await page.locator('#login').click();
const popup = await popupPromise;

Try / catch

try {
  const popup = await ctx.waitForEvent('page', predicate, 30000);
} catch (e) {
  if (String(e.message).includes('timed out')) {
    // no qualifying page appeared: verify the trigger ran and the popup opens in THIS context
    throw new Error(`no new page within timeout; popup blocked or wrong context: ${e.message}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Waiting for a popup/new tab that never opens: the click that should trigger it didn't happen or was blocked; the page opens in a DIFFERENT browser context; the predicate keeps returning false (e.g. wrong URL match); timeout shorter than the app's real popup delay.

Common situations: OAuth/social-login popups that stopped opening after a UI change; popup blockers or target=_blank links handled in the same tab; flaky CI where the site is slow and the default timeout is too small; predicates matching on a URL the app no longer uses.

Understand the failure class

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/ffaaf83cd4e70882. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:383

	}

	evCancelCtx, evCancelFn := context.WithCancel(b.ctx)
	defer evCancelFn() // This will remove the event handler once we return from here.

	chEvHandler := make(chan Event)
	ch := make(chan any)
	errCh := make(chan error)

	go b.runWaitForEventHandler(evCancelCtx, chEvHandler, predicateFn, ch, errCh)

	b.on(evCancelCtx, []string{EventBrowserContextPage}, chEvHandler)

	select {
	case <-b.ctx.Done():
		return nil, ContextErr(b.ctx) //nolint:wrapcheck
	case <-time.After(timeout):
		b.logger.Debugf("BrowserContext:WaitForEvent:timeout", "bctxid:%v event:%q", b.id, event)
		return nil, fmt.Errorf("waitForEvent timed out after %v", timeout)
	case evData := <-ch:
		b.logger.Debugf("BrowserContext:WaitForEvent:evData", "bctxid:%v event:%q", b.id, event)
		return evData, nil
	case err := <-errCh:
		b.logger.Debugf("BrowserContext:WaitForEvent:err", "bctxid:%v event:%q, err:%v", b.id, event, err)
		return nil, err
	}
}

// runWaitForEventHandler can work with a nil predicateFn. If predicateFn is
// nil it will return the response straight away.
func (b *BrowserContext) runWaitForEventHandler(
	ctx context.Context,
	chEvHandler chan Event, predicateFn func(p *Page) (bool, error),
	out chan<- any, errOut chan<- error,
) {
	b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():starts", "bctxid:%v", b.id)
	defer b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():returns", "bctxid:%v", b.id)

View on GitHub (pinned to 01ffac6f24)