{"record":{"id":"a5eb4d5212c76d22","repo":"grafana/k6","slug":"getting-original-viewport-size-w","errorCode":null,"errorMessage":"getting original viewport size: %w","messagePattern":"getting original viewport size: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":293,"sourceCode":"\t\t\t\"Screenshotter::screenshot\",\n\t\t\t\"chrome browser returned nil on page.getLayoutMetrics, falling back to defaults for cssVisualViewport \"+\n\t\t\t\t\"(scale: %v, pageX: %v, pageY: %v).\"+\n\t\t\t\t\"This is non-standard behavior, if possible please report this issue (with a reproducible script) \"+\n\t\t\t\t\"to the https://github.com/grafana/k6/issues/new.\",\n\t\t\tvisualViewportScale, visualViewportPageX, visualViewportPageY,\n\t\t)\n\t}\n\n\treturn visualViewportScale, visualViewportPageX, visualViewportPageY, nil\n}\n\nfunc (s *screenshotter) screenshotElement(\n\th *ElementHandle, opts *ElementHandleScreenshotOptions,\n) (buf []byte, err error) {\n\tformat := opts.Format\n\tviewportSize, err := s.originalViewportSize(h.frame.page)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting original viewport size: %w\", err)\n\t}\n\toriginalSize := h.frame.page.emulatedSize\n\n\terr = h.waitAndScrollIntoViewIfNeeded(h.ctx, false, true, opts.Timeout)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"scrolling element into view: %w\", err)\n\t}\n\n\tbbox, err := h.boundingBox()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"node is either not visible or not an HTMLElement: %w\", err)\n\t}\n\tif bbox.Width <= 0 {\n\t\treturn nil, fmt.Errorf(\"node has 0 width\")\n\t}\n\tif bbox.Height <= 0 {\n\t\treturn nil, fmt.Errorf(\"node has 0 height\")\n\t}","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/screenshotter.go#L275-L311","documentation":"First step of elementHandle.screenshot(): the screenshotter calls originalViewportSize(page) to know the current viewport (evaluating window dimensions if the page has none recorded), and this wraps its failure. The underlying error is either the window.innerWidth/innerHeight evaluation failing or its conversion failing — the same causes as the 'getting viewport dimensions'/'unpacking window size' errors, but reached from the element-screenshot entry point.","triggerScenarios":"await elementHandle.screenshot() on a viewport:null context while the page is navigating, closing, or crashed, so the viewport fallback evaluation fails.","commonSituations":"Element screenshots late in a test while the browser tears down; crashed renderer in CI; screenshots racing redirects.","solutions":["Check the wrapped cause (context destroyed / target closed) and screenshot while the page is definitely alive.","Wait for the element and lifecycle state before capturing.","Launch with an explicit viewport to skip the evaluation path.","Stabilize the browser environment (memory, shm) if the renderer crashed."],"exampleFix":"// before\nawait page.locator('#chart').screenshot({ path: 'chart.png' }); // right after goto\n\n// after\nconst chart = page.locator('#chart');\nawait chart.waitFor({ state: 'visible' });\nawait chart.screenshot({ path: 'chart.png' });","handlingStrategy":"validation","validationCode":"if (page.isClosed()) throw new Error('page closed');\nawait el.waitFor({ state: 'visible' }); // element present before element screenshot","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Launch with an explicit viewport to skip the window-dimension fallback.","Capture element screenshots while the page is settled and alive."],"tags":["k6","browser","screenshot","element-handle","viewport"],"backgroundTag":"execution-context-destroyed","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"}