import { mkdtemp, readdir, rm, statfs } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { test as base } from "@playwright/test"; export { expect } from "@playwright/test"; /** * The browser tests' base `test`: Playwright's, with each Firefox worker's * browser profile on a RAM filesystem. Every browser spec reaches it, through * tests/support/browser/strict-browser-test.ts or directly. * * Firefox keeps a test context's localStorage and IndexedDB in the profile on * disk (Chromium and WebKit keep them in memory), and the first localStorage * access of each context waits for that disk work. On desktop, whose SSD the * CI runner shares with every other job, that access took up to 8.2 s with * the profile in /tmp and never more than 17 ms with it in /dev/shm * (2026-11-13, six browsers at a time, 384 accesses each way). The app reads * the colour scheme preference before it renders, so a Firefox test spent the * wait inside its 5 s wait for the first screen: 19 of 463 Firefox app boots * took 0.7 to 3.8 s in eight full FE-GATE-008 runs, and none of 174 took more * than 14 ms in three Firefox runs with the profiles in RAM. The failures it * caused before were in `old-tab-chunk-recovery.spec.ts`, * `preload-use.spec.ts` and `platform-overview.spec.ts`. */ /** `f_type` of a tmpfs in statfs(2). */ const TMPFS_MAGIC = 0x0102_1994; /** The RAM filesystem of Linux, the desktop and the CI job containers included. */ const SHARED_MEMORY = "/dev/shm"; /** * Room for one Firefox profile. Six Firefox workers' profiles held 211 MB * together at their largest in a Firefox FE-GATE-008 run (2026-11-13). */ const PROFILE_ROOM_BYTES = 64 * 1024 * 1024; type ProfileRoot = Readonly<{ /** The directory for the profile when it is not the temp directory. */ root?: string; /** Why the profile stays on disk. */ note?: string; }>; /** * Where this worker's Firefox makes its profile: the temp directory when it is * already in RAM, else a new directory in /dev/shm when that has room for * every worker's profile, else the temp directory on disk, with the reason. */ async function firefoxProfileRoot(workers: number): Promise { const needed = workers * PROFILE_ROOM_BYTES; const notes: string[] = []; for (const candidate of [tmpdir(), SHARED_MEMORY]) { const space = await statfs(candidate).catch(() => undefined); if (space === undefined) { notes.push(`${candidate} is missing`); } else if (space.type !== TMPFS_MAGIC) { notes.push(`${candidate} is on disk`); } else if (space.bavail * space.bsize < needed) { notes.push( `${candidate} has ${Math.floor((space.bavail * space.bsize) / 2 ** 20)} MB free, ` + `${workers} workers need ${needed / 2 ** 20} MB`, ); } else { return candidate === tmpdir() ? {} : { root: await mkdtemp(path.join(candidate, "playwright-firefox-")) }; } } return { note: `Firefox profiles stay on disk in ${tmpdir()}: ${notes.join("; ")}.` }; } /** TMPDIR as the worker started, put back once the browser has launched. */ const workerTmpdir = process.env.TMPDIR; function restoreTmpdir() { if (workerTmpdir === undefined) delete process.env.TMPDIR; else process.env.TMPDIR = workerTmpdir; } export const test = base.extend({ firefoxProfileRoot: [ async ({ browserName }, use, workerInfo) => { const choice = browserName === "firefox" ? await firefoxProfileRoot(workerInfo.config.workers) : {}; if (choice.note !== undefined) console.warn(choice.note); if (choice.root !== undefined) process.env.TMPDIR = choice.root; try { await use(choice); } finally { restoreTmpdir(); if (choice.root !== undefined) await rm(choice.root, { recursive: true, force: true }); } }, { scope: "worker" }, ], // Playwright sets up a fixture's dependencies in the order they are named, // so the root is chosen before Playwright's own `browser` launches and // makes its profile under os.tmpdir(). TMPDIR points at the root only until // then: nothing else the worker puts in the temp directory goes to RAM. browser: [ async ({ firefoxProfileRoot, browser }, use) => { restoreTmpdir(); const { root } = firefoxProfileRoot; if (root !== undefined && (await readdir(root)).length === 0) { throw new Error( `Firefox did not make its profile in ${root}: Playwright no longer reads ` + "os.tmpdir() when it launches a browser (or the browser was connected, not launched).", ); } await use(browser); }, { scope: "worker" }, ], });