0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 20:29:55 +08:00
discourse/spec/system/helpers
Alan Guo Xiang Tan 9e83a1b304
DEV: Reuse the Playwright browser context between system specs (#41304)
This PR adds a `CAPYBARA_PLAYWRIGHT_SOFT_RESET` env flag which reuses
the Playwright browser context between system specs so each example no
longer pays the full bundle load cost.

`capybara-playwright-driver` already keeps the browser process alive,
but its reset closes the context after every example. That throws away
the context-level HTTP cache and other browser work cached with the
context, so the next example pays the bundle load cost again.

Key changes:
* Add `PlaywrightSoftReset` in `spec/support/system/capybara_patches.rb`
so `Capybara::Playwright::Driver#reset!` clears storage, cookies, and
permissions instead of closing the context. This keeps per-example
isolation while leaving the browser caches warm.
* Detect installed fake clocks by probing `setTimeout.toString()` on the
fresh page during the soft reset. Playwright's clock is context-scoped
and cannot be uninstalled, so any example that installed one (via
`BrowserTime.freeze` or `pw_page.clock.install` directly) automatically
gets a full context reset without having to declare anything.
* Detect downloads by listening for the context's `download` event.
Contexts that produced downloads fall back to a full reset so
Playwright's own artifact lifecycle handles cleanup, avoiding races with
reused contexts.
* Clear `ExtraLocalesController`'s bundle digest cache in `TestSetup`
whenever leftover translation overrides are detected (the same branch
that already calls `I18n.reload!`). Overrides change locale bundle
contents but specs that create them with `create!`/`update_columns`
bypass the digest invalidation that `TranslationOverride.upsert!`
performs, so a warm HTTP cache could serve stale bundles. A fresh digest
changes the bundle URL, which bypasses every cache naturally.
* Store downloads under a worker-local `Downloads::FOLDER` so per-worker
cleanup cannot delete another parallel worker's artifacts.
* Block service workers in system specs (`serviceWorkers: "block"`).
Letting each example register a service worker that the soft reset then
force-clears caused mass spec timeouts on CI. The exact cause requires
further investigation, but system specs currently do not rely on service
workers so blocking them is an acceptable trade-off.
* Measure the impact on a 16-vCPU DO droplet over three rounds, where
the core system suite dropped from ~843s to ~721s (~14.5%).

The optimization is opt-in via `CAPYBARA_PLAYWRIGHT_SOFT_RESET=1`, which
CI sets only for the core system and chat plugin system jobs while the
optimization proves itself.
2026-07-03 09:07:32 +08:00
..
downloads.rb DEV: Reuse the Playwright browser context between system specs (#41304) 2026-07-03 09:07:32 +08:00