grafana/k6 · error

incorrect event %q, %q is the only event supported

Error message

incorrect event %q, %q is the only event supported

What it means

Thrown by BrowserContext.waitForEvent when the requested event name is not the single supported value. The internal waitForEventType comparison only accepts waitForEventTypePage ("page"); any other string (or a mistyped variant) is rejected immediately, before any waiting starts. This mirrors the fact that the k6 browser context API only implements the page-creation event.

Source

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

// Timeout will return the default timeout or the one set by the user.
func (b *BrowserContext) Timeout() time.Duration {
	return b.timeoutSettings.timeout()
}

// WaitForEvent waits for event.
func (b *BrowserContext) WaitForEvent(event string, f func(p *Page) (bool, error), timeout time.Duration) (any, error) {
	b.logger.Debugf("BrowserContext:WaitForEvent", "bctxid:%v event:%q", b.id, event)

	return b.waitForEvent(waitForEventType(event), f, timeout)
}

func (b *BrowserContext) waitForEvent(
	event waitForEventType,
	predicateFn func(p *Page) (bool, error),
	timeout time.Duration,
) (any, error) {
	if event != waitForEventTypePage {
		return nil, fmt.Errorf("incorrect event %q, %q is the only event supported", event, waitForEventTypePage)
	}

	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)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use the only supported event: browserContext.waitForEvent('page').
  2. For other events, use the page-level API instead, e.g. page.waitForEvent('console') or page.waitForEvent('popup') on a Page object.
  3. If you truly need context-level events beyond 'page', file a feature request or use a k6 browser extension; there is no workaround parameter.

Example fix

// before
const ctx = browser.newContext();
ctx.waitForEvent('console'); // throws

// after
const ctx = browser.newContext();
ctx.waitForEvent('page'); // supported
// or, for console messages, go through a page:
const page = ctx.newPage();
page.on('console', (msg) => console.log(msg.text()));
Defensive patterns

Strategy: validation

Validate before calling

const SUPPORTED_CONTEXT_EVENTS = new Set(['page']);
function assertSupportedContextEvent(event) {
  if (!SUPPORTED_CONTEXT_EVENTS.has(event)) {
    throw new Error(
      `unsupported context event "${event}"; only "page" is supported, use page.waitForEvent for other events`
    );
  }
}
assertSupportedContextEvent(event);
context.waitForEvent(event);

Type guard

function isSupportedContextEvent(event) {
  return event === 'page';
}

Prevention

When it happens

Trigger: Calling browserContext.waitForEvent('console'), 'close', 'pageopen', 'Page', or any string other than exactly 'page'. The check happens up front, so the error is deterministic and unrelated to timing or browser state.

Common situations: Developers copying Playwright examples that wait for events like 'console' or 'backgroundpage' which k6's browser context does not support; capitalization or spelling mistakes such as 'Page' or 'newpage'; assuming the full Playwright event surface exists in k6.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/1407a0ba05fcc8ef. Report an issue: GitHub.