type: rule
status: active
timestamp: 2026-06-24
tags: [rule, automation, playwright, testing, development]

Playwright Persistent Browser Context Rules

Playwright persistent auth, cookie encryption, memory leak prevention

Playwright Persistent Browser Context Rules

To build stable, secure, and long-running browser automation (such as agent testing and voting systems) that runs for hours on local dev servers or cloud workers without losing authentication states, follow these constraints.


1. Browser Choice: Chrome vs. Microsoft Edge


[!WARNING] Do not copy raw userDataDir folders across different operating systems or user accounts. Chromium encrypts cookies, local storage, and credentials using host OS APIs (DPAPI on Windows, Keychain on macOS, Gnome Keyring/KWallet on Linux). If you copy a local Windows profile to a Linux GitHub Actions runner, the runner cannot decrypt the database. The browser will treat cookies as corrupted and wipe them, causing authentication failures.

The Storage State Solution

To persist logins across environments, use Playwright’s native Storage State format instead of raw directory copying:

  1. Generate State Locally: Run a local/headed script once to perform authentication or let the user sign in manually.
  2. Export Decrypted State: Export cookies and localStorage to a plain JSON file:
    await context.storageState({ path: 'state.json' });
  3. Deploy State to Worker: Send the state.json file (or store its text content in a secure environment variable/GitHub Secret) to the remote/cloud worker.
  4. Inject State on Launch: Launch the remote context by loading the JSON file. This bypasses OS-level encryption entirely and works cross-platform:
    const context = await browser.newContext({ storageState: 'state.json' });

3. Persistent Directory Locking Errors


4. Stability Rules for Long-Running Loops (“Hours and Hours”)

If a script runs an automation loop for hours, it will crash due to memory leaks and resource depletion unless these rules are followed:


Edit on GitHub · Back to index