{"record":{"id":"ec54461e4567a704","repo":"grafana/k6","slug":"getting-viewport-dimensions-w","errorCode":null,"errorMessage":"getting viewport dimensions: %w","messagePattern":"getting viewport dimensions: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":153,"sourceCode":"\treturn &size, nil\n}\n\nfunc (s *screenshotter) originalViewportSize(p *Page) (*Size, error) {\n\tviewportSize := p.viewportSize()\n\tif viewportSize.Width != 0 || viewportSize.Height != 0 {\n\t\treturn &viewportSize, nil\n\t}\n\n\topts := evalOptions{\n\t\tforceCallable: true,\n\t\treturnByValue: true,\n\t}\n\tresult, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, `\n\t() => (\n\t\t{ width: window.innerWidth, height: window.innerHeight }\n\t)`)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting viewport dimensions: %w\", err)\n\t}\n\n\tvar returnVal Size\n\tif err := convert(result, &returnVal); err != nil {\n\t\treturn nil, fmt.Errorf(\"unpacking window size: %w\", err)\n\t}\n\n\tviewportSize.Width = returnVal.Width\n\tviewportSize.Height = returnVal.Height\n\n\treturn &viewportSize, nil\n}\n\nfunc (s *screenshotter) restoreViewport(p *Page, originalSize *EmulatedSize) error {\n\tctx, cancel := s.cleanupContext()\n\tdefer cancel()\n\tif originalSize != nil {\n\t\treturn p.setEmulatedSize(ctx, originalSize)","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/screenshotter.go#L135-L171","documentation":"Thrown when a screenshot needs the viewport dimensions (because the page was opened without an explicit viewport size, so originalViewportSize falls back to evaluating window.innerWidth/window.innerHeight) and that evaluation fails. The error wraps whatever the frame evaluate returns — typically a destroyed execution context or a closed target.","triggerScenarios":"Taking any screenshot on a browser context launched with viewport: null (real viewport mode), while the page is navigating, crashing, or closing, so the `() => ({width: window.innerWidth, height: window.innerHeight})` evaluation cannot run.","commonSituations":"Launching with options.viewport = null to get the real screen size and then screenshotting during/after teardown; renderer crash in low-memory CI containers; screenshot racing a redirect.","solutions":["Check the wrapped cause — 'target closed'/'context destroyed' means the page died; fix the crash or call screenshot earlier.","Take screenshots after the page reaches a stable lifecycle state.","If you don't need the real viewport, launch with an explicit viewport so this code path is skipped entirely.","Increase container memory / use --disable-dev-shm-usage if Chrome is crashing."],"exampleFix":"// before (viewport: null path, screenshot during churn)\nawait page.screenshot({ path: 'home.png' });\n\n// after\nawait page.waitForLoadState('load');\nawait page.screenshot({ path: 'home.png' });","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) throw new Error('page closed before screenshot');\nawait page.waitForLoadState('load');","typeGuard":null,"tryCatchPattern":"try {\n  await page.screenshot({ path: 's.png' });\n} catch (e) {\n  if (/getting viewport dimensions.*(target closed|context destroyed)/i.test(String(e))) {\n    /* page died: report env issue, do not retry */\n  } else throw e;\n}","preventionTips":["Launch with an explicit viewport to skip the window-evaluation fallback entirely.","Screenshot while the page is alive and settled, not during teardown.","Fix renderer crashes (memory, shm) rather than retrying."],"tags":["k6","browser","screenshot","viewport","evaluation"],"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"}