存储与认证
🌐 Storage & Authentication
管理 cookies、本地存储 (localStorage)、会话存储 (sessionStorage),并保存/恢复完整的浏览器状态以保持身份验证持续性。
🌐 Manage cookies, localStorage, sessionStorage, and save/restore full browser state for authentication persistence.
存储状态(Cookies + 本地存储)
🌐 Storage state (cookies + localStorage)
将完整的浏览器状态(cookies + localStorage)保存和恢复到一个文件中。这是跨会话保持身份验证的主要方法。
🌐 Save and restore the full browser state (cookies + localStorage) in a single file. This is the primary way to persist authentication across sessions.
playwright-cli state-save # save to storage-state-{timestamp}.json
playwright-cli state-save auth.json # save to a named file
playwright-cli state-load auth.json # restore from file
这个文件保存了浏览器访问过的每个网站的 cookies 和 localStorage。绝对不要提交这些文件——它们包含活跃的认证令牌。
🌐 The file holds the cookies plus the localStorage of every origin the browser visited. Never commit these files — they carry live authentication tokens.
工作流程:保存登录,下次跳过
🌐 Workflow: save login, skip it next time
# Login once
playwright-cli open https://app.example.com/login
playwright-cli fill e3 "user@example.com"
playwright-cli fill e5 "password123"
playwright-cli click e7
# Save authenticated state
playwright-cli state-save auth.json
# Later, skip login entirely
playwright-cli state-load auth.json
playwright-cli goto https://app.example.com/dashboard
# Already logged in!
Cookies
| 命令 | 描述 |
|---|---|
cookie-list [--domain] [--path] | 列出 cookies(可按域或路径过滤) |
cookie-get <name> | 获取特定 cookie |
cookie-set <name> <value> [options] | 设置 cookie |
cookie-delete <name> | 删除 cookie |
cookie-clear | 清除所有 cookies |
cookie 设置选项
🌐 cookie-set options
| 选项 | 描述 |
|---|---|
--domain=<domain> | Cookie 域名 |
--path=<path> | Cookie 路径(默认: /) |
--expires=<timestamp> | 过期时间,Unix 时间戳 |
--httpOnly | 仅限 HTTP 标志 |
--secure | 安全标志 |
--sameSite=<value> | Strict、Lax 或 None |
$ playwright-cli cookie-list
# Name Value Domain HttpOnly Secure Expires
# session_id abc123 .example.com true true 2024-12-31
# theme dark .example.com false false Session
$ playwright-cli cookie-list --domain=.github.com
$ playwright-cli cookie-get session_id
$ playwright-cli cookie-set theme light
$ playwright-cli cookie-set session abc123 --domain=.example.com --secure --httpOnly
$ playwright-cli cookie-set remember_me token123 --expires=1893456000 --sameSite=Lax
$ playwright-cli cookie-delete session_id
$ playwright-cli cookie-clear
工作流:通过清除 cookies 测试注销
🌐 Workflow: test logout by clearing cookies
playwright-cli cookie-clear
playwright-cli reload
playwright-cli snapshot
# - heading "Sign in" [level=1]
# - textbox "Email" [ref=e3]
localStorage
| 命令 | 描述 |
|---|---|
localstorage-list | 列出所有键值对 |
localstorage-get <key> | 根据键获取值 |
localstorage-set <key> <value> | 设置值 |
localstorage-delete <key> | 删除一个键 |
localstorage-clear | 清除所有 |
$ playwright-cli localstorage-list
# Key Value
# user_preferences {"theme":"dark","lang":"en"}
# onboarding_done true
$ playwright-cli localstorage-get user_preferences
# {"theme":"dark","lang":"en"}
$ playwright-cli localstorage-set onboarding_done "false"
$ playwright-cli reload
# Onboarding wizard appears
sessionStorage
与 sessionstorage- 前缀相同的命令。标签关闭时数据将被清除。
🌐 Same commands with sessionstorage- prefix. Data is cleared when the tab closes.
playwright-cli sessionstorage-list
playwright-cli sessionstorage-get <key>
playwright-cli sessionstorage-set <key> <value>
playwright-cli sessionstorage-delete <key>
playwright-cli sessionstorage-clear
批量操作
🌐 Bulk operations
单键命令涵盖了常见情况。对于更大的批量的操作——一次添加多个 cookie,或者写入一批 localStorage 键——使用 run-code:
🌐 The single-key commands cover the common cases. For anything bulkier — adding several cookies at
once, or writing a batch of localStorage keys — use run-code:
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: '/' }
]);
}"
IndexedDB 没有专门的命令;在那里也可以使用 run-code:
🌐 IndexedDB has no dedicated commands; reach for run-code there too:
playwright-cli run-code "async page => page.evaluate(() => indexedDB.databases())"