Add `opfs=` argument to `Save Storage State`

未关闭
#5,501 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
54/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
playwright, python
领域
testing

调研方向

Read Browser/keywords/playwright_state.py methods save_storage_state and _save_storage_state, then trace the request through protobuf/playwright.proto and node/playwright-wrapper/playwright-state.ts. Add the named opfs option and pass it to Playwright's storageState() call, update the Set Storage State docstring, and add OPFS cases to atest/test/01_Browser_Management/storage_state.robot for save, restore through both APIs, and saving without OPFS; skip or tag ephemeral WebKit contexts.

由索引模型根据 Issue 内容生成。

描述

Use case

Save Storage State saves cookies and local storage, and with indexedDB=True and credentials=True also IndexedDB and virtual WebAuthn credentials (#5117). Some applications keep their data in the origin private file system (OPFS) instead: offline-first apps, apps that run SQLite in the browser (SQLite WASM persists to OPFS), and editors that store drafts as files. For those, the saved state misses the data the app needs, and the "set up once, reuse in every test" pattern does not work.

Playwright 1.63 added an opfs option to browserContext.storageState(). The state file then carries the OPFS entries of each origin, and both browser.newContext({ storageState }) and browserContext.setStorageState() restore them.

Proposed keyword / arguments

Save Storage State path=None * indexedDB=False credentials=False opfs=False

  • opfs: named-only, defaults to False. Also saves the origin private file system of every origin in the state.
*** Test Cases ***
Prepare Offline Data Once
    New Context
    New Page    ${APP_URL}
    Click    id=download-for-offline
    Get Text    id=offline-status    ==    Ready
    Save Storage State    ${CURDIR}/state/offline.json    opfs=True

Use Offline Data
    New Context    storageState=${CURDIR}/state/offline.json
    New Page    ${APP_URL}
    Get Text    id=offline-status    ==    Ready

New Context storageState= and Set Storage State need no new argument. They restore whatever the file contains.

Playwright API

Checked with Playwright 1.65.0 and Chromium: with opfs: true each origin in the state gets an opfs list of {path, type: "directory"} and {path, type: "file", base64} entries. A new context created with that state, and an existing context after setStorageState(), both read the file back. Without opfs: true no opfs key is written.

Implementation notes

All references are to upstream main (5e350998).

  • protobuf/playwright.proto:137-141 Request.StorageState: add bool opfs = 4;. The rpc stays SaveStorageState (:713).
  • node/playwright-wrapper/playwright-state.ts:1384-1388 saveStorageState: pass opfs: request.opfs to context.c.storageState(), next to indexedDB and credentials.
  • Browser/keywords/playwright_state.py:2106-2112 save_storage_state: add named-only opfs: bool = False; pass it through _save_storage_state (:2176-2185) into Request().StorageState(...). Docstring: one line in Arguments, plus the WebKit limitation. New Context is an ephemeral context, so in WebKit opfs=True works only after New Persistent Context.
  • Browser/keywords/playwright_state.py:2196-2197 set_storage_state docstring: "Clears the cookies, local storage, IndexedDB and virtual WebAuthn credentials" should name OPFS too. Playwright clears it on every setStorageState(), also when the file has no opfs entries: in the check above, restoring a state saved without opfs removed the file the page had written. That is Playwright's behaviour since 1.63 and already happens with the current keyword, so the docstring fix stands on its own even without opfs=.
  • Set Storage State has IndexedDB-specific handling (reload_pages, indexedDbOrigins at playwright-state.ts:1392-1402, the open-connection probe at :1412-1432) because deleting an IndexedDB database waits for open connections. Playwright clears OPFS with removeEntry(name, { recursive: true }) and writes files with createWritable(). I have not found or tested a similar blocking case for OPFS. A file locked by a FileSystemSyncAccessHandle in a worker might make the clear fail instead of hang; worth one acceptance test before deciding whether reload_pages needs to cover OPFS origins. Until then, no change there.
  • Tests: atest/test/01_Browser_Management/storage_state.robot has the IndexedDB cases as Evaluate JavaScript snippets in its Variables (:9-14). Add matching OPFS write and read snippets and test: save with opfs=True, restore with New Context storageState=, restore with Set Storage State, and save without opfs leaves no opfs key in the file. Skip or tag the WebKit case, since the suite runs in ephemeral contexts.

Backwards compatibility

Additive, named-only argument that defaults to False. State files written without it are unchanged. The docstring change to Set Storage State documents behaviour that is already there.

主要语言
Python
星标
662
派生
149
平均合并
6 小时 13 分钟
30 天内合并 PR
121

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MarketSquare/robotframework-browser 的其他 Issue

查看 MarketSquare/robotframework-browser 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。