{
  "slug": "k6-browser-chromium-connectovercdp-attaches-to-a-running-browser-9dcfde742a28",
  "title": "k6/browser chromium.connectOverCDP() attaches to a running browser",
  "tools": [
    "k6"
  ],
  "tags": [
    "agent"
  ],
  "introduced_at": "2026-08-10T14:01:35.000Z",
  "announced_at": "2026-08-10T14:01:35.000Z",
  "effective_at": null,
  "date_basis": "source-publication",
  "status": "unknown",
  "how": {
    "incantation": "When a test needs to attach to an existing Chromium-based browser, pass its CDP WebSocket endpoint to `chromium.connectOverCDP()` inside the iteration and use the returned browser. k6 closes the connection at iteration end; call `browser.close()` to close it earlier.",
    "examples": [
      "import { chromium } from 'k6/browser';\n\nexport default async function () {\n  const browser = await chromium.connectOverCDP('ws://localhost:9222/devtools/browser/<BROWSER_ID>');\n  const page = await browser.newPage();\n  try {\n    await page.goto('https://quickpizza.grafana.com/');\n  } finally {\n    await page.close();\n    await browser.close();\n  }\n}",
      "import http from 'k6/http';\nimport { chromium } from 'k6/browser';\n\nexport function setup() {\n  const res = http.post('https://provider.example/v1/sessions');\n  return { wsURL: res.json().connectUrl };\n}\n\nexport default async function (data) {\n  const browser = await chromium.connectOverCDP(data.wsURL);\n  const page = await browser.newPage();\n  try {\n    await page.goto('https://quickpizza.grafana.com/');\n  } finally {\n    await page.close();\n    await browser.close();\n  }\n}"
    ],
    "gotchas": [
      "Version: v2.2.0",
      "wsEndpoint (required): String containing the browser's CDP WebSocket endpoint; documentation says it must use `ws` or `wss`.",
      "browser.close(): Closes the connection early; k6 otherwise closes it at iteration end. Closing the connection does not stop the browser.",
      "Prerequisite: An existing Chromium-based browser with a CDP WebSocket endpoint. For the local example, start Chrome with `--remote-debugging-port` and read `webSocketDebuggerUrl` from `http://localhost:9222/json/version`.",
      "Prerequisite: The function connects within an iteration and does not require the `browser` scenario option.",
      "Attaches to Chromium-based browsers over the Chrome DevTools Protocol."
    ]
  },
  "why_it_matters": "When a browser test needs to attach to an existing Chromium-based browser, connect to its WebSocket endpoint and run the iteration against it.",
  "sources": [
    "https://github.com/grafana/k6/releases/tag/v2.2.0",
    "https://github.com/grafana/k6/blob/v2.2.0/release%20notes/v2.2.0.md",
    "https://grafana.com/docs/k6/latest/javascript-api/k6-browser/chromium/connectovercdp/"
  ],
  "rank": 0.95027,
  "judge": {
    "publish": true,
    "judge_model": "codex-cli/gpt-6-luna+typesafe/jev-1.13.0",
    "prompt_version": "extract-v7+enrich-v2+judge-v2x3+jev-publish-v1"
  },
  "review_state": "approved",
  "provenance": {
    "run_id": "wave2a-20261017",
    "verdict_id": "wave2a-20261017:wave2a-20261017-c50b860ccf230af2-1:gate",
    "evidence_hash": "5029169f942862caa4fcc22c402f7f04b7ddcfb75af24cf8ee5e89a592ecd7bf"
  },
  "review": {
    "draft_hash": "b6aee4b15c9f723c6cad6e23acccd52babc75897a0380e1e25f850b652c0b829",
    "reviewer": "publication-gate",
    "reviewed_at": "2026-10-07T00:00:00.000Z",
    "decision": "wave2a-20261017:gate",
    "reason": "3 of 3 judge runs approved with p_yes >= 0.9 (mean 0.95 >= 0.9; 2 needed) and Jev gave 0.74 (>= 0.6 needed)."
  }
}
