{"record":{"id":"8a7d6b0273fe232c","repo":"grafana/k6","slug":"setting-viewport-size-w","errorCode":null,"errorMessage":"setting viewport size: %w","messagePattern":"setting viewport size: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":1764,"sourceCode":"\tp.logger.Debugf(\"Page:SetExtraHTTPHeaders\", \"sid:%v\", p.sessionID())\n\n\tp.extraHTTPHeaders = headers\n\treturn p.updateExtraHTTPHeaders()\n}\n\n// SetInputFiles sets input files for the selected element.\nfunc (p *Page) SetInputFiles(selector string, files *Files, opts *FrameSetInputFilesOptions) error {\n\tp.logger.Debugf(\"Page:SetInputFiles\", \"sid:%v selector:%s\", p.sessionID(), selector)\n\n\treturn p.MainFrame().SetInputFiles(selector, files, opts)\n}\n\n// SetViewportSize will update the viewport width and height.\nfunc (p *Page) SetViewportSize(viewportSize *Size) error {\n\tp.logger.Debugf(\"Page:SetViewportSize\", \"sid:%v\", p.sessionID())\n\n\tif err := p.setViewportSize(p.ctx, viewportSize); err != nil {\n\t\treturn fmt.Errorf(\"setting viewport size: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// Tap will tap the element matching the provided selector.\nfunc (p *Page) Tap(selector string, opts *FrameTapOptions) error {\n\tp.logger.Debugf(\"Page:SetViewportSize\", \"sid:%v selector:%s\", p.sessionID(), selector)\n\n\treturn p.MainFrame().Tap(selector, opts)\n}\n\n// TextContent returns the textContent attribute of the first element found\n// that matches the selector. The second return value is true if the returned\n// text content is not null or empty, and false otherwise.\nfunc (p *Page) TextContent(selector string, popts *FrameTextContentOptions) (string, bool, error) {\n\tp.logger.Debugf(\"Page:TextContent\", \"sid:%v selector:%s\", p.sessionID(), selector)\n","sourceCodeStart":1746,"sourceCodeEnd":1782,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L1746-L1782","documentation":"Thrown by Page.SetViewportSize in the k6 browser module when the page fails to apply a new viewport size. It wraps the error from the internal setViewportSize -> setEmulatedSize chain, which ultimately drives the CDP Emulation.setDeviceMetricsOverride call on every frame session. The outer message 'setting viewport size' only adds context; the underlying error names the real cause (CDP/protocol failure, closed target, or invalid size).","triggerScenarios":"Calling page.setViewportSize({width, height}) (or any API that resizes the viewport, e.g. element screenshots that enlarge the viewport for oversized elements via screenshotElement's overriddenViewportSize path) while the page/target is closing, on a crashed browser, or after the CDP session is torn down; passing NaN or negative dimensions is also surfaced through this wrapper.","commonSituations":"Resizing the viewport right before or during navigation/closure races the target being destroyed; running with a browser that crashed mid-test (OOM in containerized Chrome); headless environment issues that kill the target; extremely large sizes that Chrome rejects.","solutions":["Check the wrapped error text after 'setting viewport size:' — it names the actual CDP failure (e.g. 'target closed', 'session detached') and dictates the fix.","Ensure the page is still open (page.isClosed() === false) before calling page.setViewportSize().","Retry the call once after a short wait if the failure happened during a navigation or page-creation race.","Verify Chrome did not crash (check k6 logs and browser process memory limits, e.g. --disable-dev-shm-usage or a bigger container shm).","Pass positive finite integers for width and height."],"exampleFix":"// before\npage.setViewportSize({ width: 1280, height: 720 }); // fails if page is mid-close\n\n// after\nif (!page.isClosed()) {\n  page.setViewportSize({ width: 1280, height: 720 });\n}","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) { /* skip resize */ }\nconst w = Number(size.width), h = Number(size.height);\nif (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0) throw new Error('invalid viewport');","typeGuard":"const isValidViewport = (s) =>\n  typeof s === 'object' && s !== null &&\n  Number.isFinite(s.width) && s.width > 0 &&\n  Number.isFinite(s.height) && s.height > 0;","tryCatchPattern":"try {\n  page.setViewportSize({ width: 1280, height: 720 });\n} catch (e) {\n  if (/target closed|session/i.test(String(e))) { /* page gone; abort flow */ }\n  else throw e;\n}","preventionTips":["Never resize during navigation or teardown; check page.isClosed() first.","Keep sizes positive integers.","Give Chrome enough memory/shm so the target does not die mid-resize."],"tags":["k6","browser","viewport","cdp","screenshot"],"backgroundTag":"cdp-target-closed","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-09-20T22:01:45.163Z","contentChangedAt":"2026-09-20T22:01:45.163Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}