grafana/k6 · error

emulating viewport

Error message

emulating viewport: %w

What it means

Wrap of the CDP Emulation.setDeviceMetricsOverride command: updateViewport computes the emulated viewport and screen size, picks a portrait/landscape orientation from the dimensions, and sends the override (width, height, deviceScaleFactor, isMobile, screen orientation/size) to the main-frame session. The wrapped error occurs when the CDP call fails — closed/canceled context, detached session, or dimensions/scale factor values the browser rejects (e.g., zero width/height).

Solutions

  1. Provide a valid Viewport with positive width and height
  2. Ensure the page and its main-frame session are still alive when changing the viewport
  3. Avoid changing viewport size while the target is navigating or closing
  4. Retry with a fresh page if the browser session was terminated
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/frame_session.go:1257 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-09-20). Data as JSON: /api/errors/d6ff2c24f2b00f2f. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/frame_session.go:1314

		return nil
	}
	viewport := emulatedSize.Viewport
	screen := emulatedSize.Screen

	orientation := emulation.ScreenOrientation{
		Angle: 0.0,
		Type:  emulation.OrientationTypePortraitPrimary,
	}
	if viewport.Width > viewport.Height {
		orientation.Angle = 90.0
		orientation.Type = emulation.OrientationTypeLandscapePrimary
	}
	action := emulation.SetDeviceMetricsOverride(viewport.Width, viewport.Height, opts.DeviceScaleFactor, opts.IsMobile).
		WithScreenOrientation(&orientation).
		WithScreenWidth(screen.Width).
		WithScreenHeight(screen.Height)
	if err := action.Do(cdp.WithExecutor(ctx, fs.session)); err != nil {
		return fmt.Errorf("emulating viewport: %w", err)
	}

	if fs.hasUIWindow {
		// add an inset to viewport depending on the operating system.
		// this won't add an inset if we're running in headless mode.
		viewport = viewport.recalculateInset(
			fs.page.browserCtx.browser.browserOpts.Headless,
			runtime.GOOS,
		)
		action2 := browser.SetWindowBounds(fs.windowID, &browser.Bounds{
			Width:  viewport.Width,
			Height: viewport.Height,
		})
		if err := action2.Do(cdp.WithExecutor(ctx, fs.session)); err != nil {
			return fmt.Errorf("setting window bounds: %w", err)
		}
	}

View on GitHub (pinned to 3fcf5388d7)