new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · all subjects

authentication/storage

20 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

storageState method to save authenticated state

Use `page.context().storageState({ path: authFile })` to save the authenticated browser state to a file after completing authentication steps.

storageState covers cookies, local storage, IndexedDB, and WebAuthn

The `storageState` method persists and restores authenticated state stored as cookies, local storage, IndexedDB, and WebAuthn credentials (passkeys). These can be used across different browsers depending on the application's authentication model.

BrowserContext.storageState method signature

Call `context.storageState({ path: 'state.json' })` to save storage state to a file, or `context.storageState()` to retrieve it as an object.

Create new context with saved storage state

Use `browser.newContext({ storageState: 'playwright/.auth/user.json' })` to create a new context with pre-populated authenticated state from a saved file.

Save browser state in run-code

Use `await page.context().storageState({ path: 'auth.json' })` to save the browser's storage state (cookies, local storage, session storage) to a file for later reuse, such as for authentication flows.

Storage state file format with cookies and origins

The saved storage state file contains two root properties: 'cookies' (array of cookie objects) and 'origins' (array of origin objects). Each cookie object has: name, value, domain, path, expires (Unix timestamp), httpOnly (boolean), secure (boolean), sameSite (string). Each origin object has: origin (string) and localStorage (array of name-value pairs).

Save storage state with playwright-cli state-save

Use 'playwright-cli state-save' to save complete browser state including cookies and storage to a file. Without a filename argument, it generates an auto-named file with pattern 'storage-state-{timestamp}.json'. With a filename argument, it saves to that specific filename.

Restore storage state with playwright-cli state-load

Use 'playwright-cli state-load <filename>' to load previously saved storage state from a file, restoring cookies and localStorage to the browser context.

List cookies with playwright-cli cookie-list

Use 'playwright-cli cookie-list' to list all cookies. Add '--domain=<domain>' flag to filter by domain. Add '--path=<path>' flag to filter by path.

Get specific cookie with playwright-cli cookie-get

Use 'playwright-cli cookie-get <cookie_name>' to retrieve a specific cookie by name.

Set cookie with playwright-cli cookie-set

Use 'playwright-cli cookie-set <name> <value>' to set a basic cookie. Add optional flags: '--domain=<domain>', '--path=<path>', '--httpOnly', '--secure', '--sameSite=<value>', '--expires=<unix_timestamp>' to customize cookie properties.

Delete and clear cookies with playwright-cli

Use 'playwright-cli cookie-delete <cookie_name>' to delete a single cookie by name. Use 'playwright-cli cookie-clear' to delete all cookies.

Multiple cookies with playwright-cli run-code

For complex scenarios like adding multiple cookies at once, use 'playwright-cli run-code' with inline code. Example: playwright-cli run-code "async page => { await page.context().addCookies([{ name: 'session_id', value: 'sess_abc123', domain: 'example.com', path: '/', httpOnly: true }, { name: 'preferences', value: JSON.stringify({ theme: 'dark' }), domain: 'example.com', path: '/' }]); }"

List localStorage items with playwright-cli localstorage-list

Use 'playwright-cli localstorage-list' to list all localStorage items in the current origin.

Get and set localStorage with playwright-cli

Use 'playwright-cli localstorage-get <key>' to retrieve a localStorage value. Use 'playwright-cli localstorage-set <key> <value>' to set a localStorage value. Values can be plain strings or JSON strings.

Delete localStorage items with playwright-cli

Use 'playwright-cli localstorage-delete <key>' to delete a single localStorage item by key. Use 'playwright-cli localstorage-clear' to delete all localStorage items.

List sessionStorage items with playwright-cli sessionstorage-list

Use 'playwright-cli sessionstorage-list' to list all sessionStorage items in the current session.

Get, set, and delete sessionStorage with playwright-cli

Use 'playwright-cli sessionstorage-get <key>' to retrieve a sessionStorage value. Use 'playwright-cli sessionstorage-set <key> <value>' to set a sessionStorage value. Use 'playwright-cli sessionstorage-delete <key>' to delete a single item. Use 'playwright-cli sessionstorage-clear' to delete all sessionStorage items.

List and delete IndexedDB databases with playwright-cli run-code

To list IndexedDB databases, use 'playwright-cli run-code "async page => { return await page.evaluate(async () => { const databases = await indexedDB.databases(); return databases; }); }"'. To delete a database, use 'playwright-cli run-code "async page => { await page.evaluate(() => { indexedDB.deleteDatabase(\'myDatabase\'); }); }"'.

Security best practices for storage state files

Never commit storage state files containing auth tokens to version control. Add '*.auth-state.json' to .gitignore. Delete state files after automation completes. Use environment variables for sensitive data. By default, sessions run in-memory mode which is safer for sensitive operations.

Give your agent this brain