Playwright CLI storage state: Save Login Sessions, Read Cookies, and Local Storage

Based on the official storage-state reference, this post summarizes the common Playwright CLI commands for storage state, Cookies, localStorage, sessionStorage, and IndexedDB with concise explanations.

If you use Playwright CLI for browser automation, storage state is one of the most useful features. Its job is simple: save the current login session and local browser state so you can reuse them later instead of logging in every time.

01 Save the current storage state

The most common workflow is to log in once, then export the current state to a file.

1
playwright-cli storage-state save auth.json

This command saves the current browser context state to auth.json. If you want to reuse a login session later, this is usually the first step.

02 Load an existing storage state

Once you already have a state file, you can load it directly at startup.

1
playwright-cli --storage-state auth.json

It launches the browser context with the state stored in auth.json. The usual purpose is to skip repeated login and jump straight into a signed-in environment.

03 View current cookies

If you only want to check which cookies exist in the current session, you can inspect them directly.

1
playwright-cli cookies

This command lists the cookies in the current context. It is useful when checking whether the login state exists or whether cookies were written successfully.

04 Set cookies

If you already have cookie data, you can inject it directly as well.

1
playwright-cli cookies set '[{"name":"session","value":"abc","domain":"example.com","path":"/"}]'

This is useful for auth debugging, reproducing a specific session, or injecting cookie conditions before a script runs.

05 Read localStorage

Some sites store login-related or frontend state not only in cookies, but also in localStorage.

1
playwright-cli local-storage

This command shows the localStorage content of the current page. It is especially useful when a page looks logged in but still behaves incorrectly.

06 Write localStorage

If you need to simulate specific frontend state, you can set it directly.

1
playwright-cli local-storage set token abc123

It writes the given key and value into localStorage. Common uses include injecting a token, a preference, or a frontend flag.

07 Read sessionStorage

sessionStorage is useful for checking temporary state tied to the current session.

1
playwright-cli session-storage

This command outputs the current page’s sessionStorage. If a page flow depends on one-time session data, this is a good place to inspect it.

08 Write sessionStorage

When needed, you can also set sessionStorage manually.

1
playwright-cli session-storage set key value

It is useful for reproducing page behavior that depends on temporary state, or for filling in fields required by an initialization step.

09 View IndexedDB

For heavier web apps, the most important local data may actually live in IndexedDB.

1
playwright-cli indexed-db

This command lets you inspect the current page’s IndexedDB data. It is worth checking first when you are dealing with complex SPAs, offline cache, or app-like local databases.

10 A practical workflow

If your goal is simply to reuse a login session reliably, the most practical flow is usually this:

  1. Open the site and complete login manually.
  2. Save the state with this command:
1
playwright-cli storage-state save auth.json
  1. Load it directly in later runs:
1
playwright-cli --storage-state auth.json

If the page still behaves incorrectly after loading, continue by checking:

  • playwright-cli cookies
  • playwright-cli local-storage
  • playwright-cli session-storage
  • playwright-cli indexed-db

That sequence covers most cases where the restored state is incomplete.

11 What to watch out for

Three points matter most:

  • A storage state file is sensitive data. It may contain login cookies or tokens, so do not commit it casually.
  • Restoring cookies alone may not be enough. Many modern sites also depend on localStorage, sessionStorage, or IndexedDB.
  • State files do not stay valid forever. After cookie expiration, account changes, or environment changes, you usually need to generate them again.

12 Quick summary

If you only remember one sentence:

Playwright CLI storage state means saving the browser’s current state and reusing it in later tasks.

In practice, the most useful commands are these:

1
2
3
4
5
6
playwright-cli storage-state save auth.json
playwright-cli --storage-state auth.json
playwright-cli cookies
playwright-cli local-storage
playwright-cli session-storage
playwright-cli indexed-db

Save first, then load; if something still looks wrong, inspect cookies and the different local storage layers one by one. That is the most practical part of this reference.

References

记录并分享
Built with Hugo
Theme Stack designed by Jimmy