{"record":{"id":"d6ff2c24f2b00f2f","repo":"grafana/k6","slug":"emulating-viewport-w","errorCode":null,"errorMessage":"emulating viewport: %w","messagePattern":"emulating viewport: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame_session.go","lineNumber":1257,"sourceCode":"\t\treturn nil\n\t}\n\tviewport := emulatedSize.Viewport\n\tscreen := emulatedSize.Screen\n\n\torientation := emulation.ScreenOrientation{\n\t\tAngle: 0.0,\n\t\tType:  emulation.OrientationTypePortraitPrimary,\n\t}\n\tif viewport.Width > viewport.Height {\n\t\torientation.Angle = 90.0\n\t\torientation.Type = emulation.OrientationTypeLandscapePrimary\n\t}\n\taction := emulation.SetDeviceMetricsOverride(viewport.Width, viewport.Height, opts.DeviceScaleFactor, opts.IsMobile).\n\t\tWithScreenOrientation(&orientation).\n\t\tWithScreenWidth(screen.Width).\n\t\tWithScreenHeight(screen.Height)\n\tif err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {\n\t\treturn fmt.Errorf(\"emulating viewport: %w\", err)\n\t}\n\n\tif fs.hasUIWindow {\n\t\t// add an inset to viewport depending on the operating system.\n\t\t// this won't add an inset if we're running in headless mode.\n\t\tviewport = viewport.recalculateInset(\n\t\t\tfs.page.browserCtx.browser.browserOpts.Headless,\n\t\t\truntime.GOOS,\n\t\t)\n\t\taction2 := browser.SetWindowBounds(fs.windowID, &browser.Bounds{\n\t\t\tWidth:  viewport.Width,\n\t\t\tHeight: viewport.Height,\n\t\t})\n\t\tif err := action2.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {\n\t\t\treturn fmt.Errorf(\"setting window bounds: %w\", err)\n\t\t}\n\t}\n","sourceCodeStart":1239,"sourceCodeEnd":1275,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame_session.go#L1239-L1275","documentation":"Viewport emulation builds a Emulation.SetDeviceMetricsOverride action from the configured width/height/deviceScaleFactor/mobile plus screen orientation and screen dimensions, and executes it on the frame's session. This error means that CDP command failed while applying the device metrics.","triggerScenarios":"Creating a page with a viewport option (or updating the viewport) with invalid dimensions such as zero or negative width/height, or executing the action when the target is closing, the browser has disconnected, or the screen-orientation/screen-size parameters are inconsistent for mobile emulation.","commonSituations":"Mobile/device emulation with unusual viewport configs; empty or defaulted viewport objects producing 0x0; calling viewport changes during teardown; headed runs on minimal window managers where screen metrics lookup returns odd values.","solutions":["Set an explicit, positive viewport (e.g. { width: 1280, height: 720 }) and a valid deviceScaleFactor","Apply the viewport at browser.newPage() time instead of changing it mid-navigation","Retry once if the failure coincided with navigation or setup races","In headed mode, ensure a real display or xvfb is available so screen metrics are sane"],"exampleFix":"// before\nconst page = await browser.newPage({ viewport: { width: 0, height: 0 } });\n\n// after\nconst page = await browser.newPage({ viewport: { width: 1280, height: 720 } });","handlingStrategy":"validation","validationCode":"const vp = { width: 1280, height: 720 };\nif (!(vp.width > 0 && vp.height > 0)) throw new Error('viewport must be positive');\nconst page = await browser.newPage({ viewport: vp });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set an explicit valid viewport at page creation","Avoid changing viewport mid-navigation or during teardown","In headed mode, ensure a display server is available so screen metrics are valid"],"tags":["browser","viewport","device-emulation","cdp"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}