{"record":{"id":"039857a7e31e2a31","repo":"grafana/k6","slug":"granting-browser-permissions-w","errorCode":null,"errorMessage":"granting browser permissions: %w","messagePattern":"granting browser permissions: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":250,"sourceCode":"\t\t\"gyroscope\":            cdpbrowser.PermissionTypeSensors,\n\t\t\"magnetometer\":         cdpbrowser.PermissionTypeSensors,\n\t\t\"clipboard-read\":       cdpbrowser.PermissionTypeClipboardReadWrite,\n\t\t\"clipboard-write\":      cdpbrowser.PermissionTypeClipboardSanitizedWrite,\n\t\t\"payment-handler\":      cdpbrowser.PermissionTypePaymentHandler,\n\t}\n\n\tperms := make([]cdpbrowser.PermissionType, 0, len(permissions))\n\tfor _, p := range permissions {\n\t\tproto, ok := permsToProtocol[p]\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"%q is an invalid permission\", p)\n\t\t}\n\t\tperms = append(perms, proto)\n\t}\n\n\taction := cdpbrowser.GrantPermissions(perms).WithOrigin(opts.Origin).WithBrowserContextID(b.id)\n\tif err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {\n\t\treturn fmt.Errorf(\"granting browser permissions: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// NewPage creates a new page inside this browser context.\nfunc (b *BrowserContext) NewPage() (*Page, error) {\n\tb.logger.Debugf(\"BrowserContext:NewPage\", \"bctxid:%v\", b.id)\n\t_, span := TraceAPICall(b.ctx, \"\", \"browserContext.newPage\")\n\tdefer span.End()\n\n\tp, err := b.browser.newPageInContext(b.id)\n\tif err != nil {\n\t\treturn nil, spanRecordErrorf(span, \"creating new page in browser context: %w\", err)\n\t}\n\n\tb.logger.Debugf(\"BrowserContext:NewPage:return\", \"bctxid:%v ptid:%s\", b.id, p.targetID)\n\treturn p, nil","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L232-L268","documentation":"After the allowlist check passes, GrantPermissions issues the CDP Browser.grantPermissions command with an optional origin (browser_context.go:250). A CDP-level failure — invalid origin, a permission unsupported by the installed chromium build, or a lost connection — is wrapped as 'granting browser permissions: %w'.","triggerScenarios":"The origin option is not a valid origin (e.g. 'localhost' without a scheme, or a URL with a path); the chromium build rejects one of the permission types; the context or connection is already closed when the call is made.","commonSituations":"Copying origin strings from Playwright tests with different normalization; older system chromium behind K6_BROWSER_EXECUTABLE_PATH; granting permissions during teardown after the browser disconnected.","solutions":["Use a full origin like 'https://example.com' — scheme plus host, no path","Verify the context is open and the browser connected before granting","Test with the chromium recommended for your k6 version and read the wrapped CDP error for chromium's exact complaint","Prefer granting permissions via newContext({ permissions: [...] }) at creation time instead of later calls"],"exampleFix":"// before\nctx.grantPermissions(['geolocation'], { origin: 'localhost:3000' }) // invalid origin\n\n// after\nctx.grantPermissions(['geolocation'], { origin: 'http://localhost:3000' })","handlingStrategy":"validation","validationCode":"const origin = 'https://example.com' // scheme + host only\nctx.grantPermissions(['geolocation'], { origin })","typeGuard":"function isValidOrigin(o) {\n  try { const u = new URL(o); return u.origin === o }\n  catch { return false }\n}","tryCatchPattern":"try {\n  ctx.grantPermissions(perms, { origin })\n} catch (e) {\n  if (/granting browser permissions/.test(String(e))) {\n    // retry with a normalized origin or without the origin option\n  } else throw e\n}","preventionTips":["Use full origins (scheme + host), no paths","Grant permissions via newContext({ permissions: [...] }) at creation time when possible","Verify chromium supports the permission you are granting"],"tags":["cdp","permissions","origin","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}