{"record":{"id":"3c3bf02a3a19edc9","repo":"vitest-dev/vitest","slug":"page-sessionid-not-found-in-this-browsernam","errorCode":null,"errorMessage":"Page \"${sessionId}\" not found in ${this.browserName} browser.","messagePattern":"Page \"(.+?)\" not found in (.+?) browser\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser-playwright/src/playwright.ts","lineNumber":570,"sourceCode":"    const contextOptions = this.options.contextOptions ?? {}\n    const options = {\n      ...contextOptions,\n      ignoreHTTPSErrors: true,\n    } satisfies BrowserContextOptions\n    // A `null` viewport lets the page adopt the real window size, which is only\n    // meaningful for a headed UI. In headless mode there is no real window, so it\n    // would inherit the host's device scale factor and produce screenshots that\n    // differ from non-UI runs on the same machine.\n    if (this.project.config.browser.ui && !this.project.config.browser.headless) {\n      options.viewport = null\n    }\n    return options\n  }\n\n  public getPage(sessionId: string): Page {\n    const page = this.pages.get(sessionId)\n    if (!page) {\n      throw new Error(`Page \"${sessionId}\" not found in ${this.browserName} browser.`)\n    }\n    return page\n  }\n\n  public getCommandsContext(sessionId: string): {\n    page: Page\n    context: BrowserContext\n    frame: () => Promise<Frame>\n    readonly iframe: FrameLocator\n  } {\n    const page = this.getPage(sessionId)\n    return {\n      page,\n      context: this.contexts.get(sessionId)!,\n      frame(): Promise<Frame> {\n        return new Promise<Frame>((resolve, reject) => {\n          const frame = page.frame('vitest-iframe')\n          if (frame) {","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser-playwright/src/playwright.ts#L552-L588","documentation":"PlaywrightBrowserProvider.getPage looks up the Page for a session id in an internal Map. If the session id was never opened, was already closed (openBrowserPage closes the prior page), or the provider was reset, the lookup returns undefined and getPage throws. getPage backs getCommandsContext, getCDPSession, and most browser command entry points.","triggerScenarios":"Using a sessionId after the page was closed (e.g., after parallel re-entry which closes the previous page), passing a wrong/stale sessionId to a command, calling browser APIs after the test/session ended, or a race where the page is still being opened.","commonSituations":"Reusing a captured sessionId across navigations that recycled the page; concurrency/parallel sessions colliding; calling page APIs in afterAll after the session was torn down; typo or mismatched sessionId between caller and provider.","solutions":["Always obtain the sessionId from the current test context, do not cache it across async boundaries that may recycle the page.","Avoid manually opening/closing pages; let vitest manage the lifecycle per test.","If running parallel tests, ensure each test uses its own session and does not reference another's sessionId.","Check that you are not calling browser commands after the test/session has finished."],"exampleFix":"// before\nconst id = currentSessionId\n// ... test ends, page recycled ...\nawait provider.getPage(id) // throws\n\n// after\n// always derive session from the active test; never reuse across tests\nconst page = provider.getCommandsContext(activeSessionId).page","handlingStrategy":"validation","validationCode":"// always derive the sessionId from the active context; do not cache\nfunction useCurrentPage(provider: { getPage: (id: string) => unknown }, sessionId: string) {\n  if (!sessionId) throw new Error('sessionId is required')\n  return provider.getPage(sessionId)\n}","typeGuard":"function hasPage(provider: { pages: Map<string, unknown> }, sessionId: string): boolean {\n  return provider.pages.has(sessionId)\n}","tryCatchPattern":"try {\n  const page = provider.getPage(sessionId)\n} catch (err) {\n  if (err instanceof Error && err.message.includes('not found')) {\n    // session was recycled; re-acquire from current test context or skip\n    return\n  }\n  throw err\n}","preventionTips":["Never cache sessionId across tests or async gaps that may recycle the page.","Let vitest manage page lifecycle; avoid manual open/close.","For parallel tests, keep each session's id isolated."],"tags":["browser","playwright","session","lifecycle","race-condition"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}