[Feature] Support "context.setStorageState()"
See original GitHub issueProposed functionality
A new setStorageState() API on the context object that applies any given storage state to the current page.
context.setStorageState({ path: '/storage.json' })
context.setStorageState({
  cookies: [...],
  origins: [...]
})
This API is opposite in its functionality to the existing context.storageState() that flushes the storage state to the disk.
Use case
The Authentication docs showcase a great example of creating a browser context, performing sign-in, and storing that session in a JSON file. However, to use that session file you need to create a new browsing context. This is not suitable when using @playwright/test and wanting to control auth state per test.
The recommended example shows that we can create separate contexts with a different session, and that works great, but you lose the ability to use the already existing context that gets created by the Playwright test runner. You also lose any custom fixtures as they are attached to that default context.
In general, I see no reason not to have the “read” functionality for the session state.
Implementation
You’re already using this logic in Playwright, it’s just internal:
I propose to abstract it into a general function, dropping internals like this. _settingStorageState, and reuse it in the new page.setStorageState() API.
Issue Analytics
- State:
- Created a year ago
- Reactions:8
- Comments:5 (1 by maintainers)

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
@mxschmitt, I believe so. I don’t have a strong opinion on what’s the best surface to apply this. Since it’s the context that holds cookies/storage, I see a
context.setStorageState()as a great API to have in addition tostorageState()(which, I believe, is also exposed onpage.context()and never on thepagedirectly).That doesn’t make the feature request invalid. Perhaps I need to rephrase it. We need an API to apply a given state JSON to any existing context. This doesn’t seem invalid in any way, both because it’s conceptually a valid use case and because it’s a matter of applying cookies and local storage items to a context, which is completely possible with the existing API (apart from the local storage, which requires to resort to
page.evaluate()).Edit: I’ve updated the feature proposal to reflect on the
contextchange.Storage state includes cookies and local storage, both of them are NOT isolated on page level, they are rather isolated on the browser context level, since the browser context is the in-memory browser profile in this case.
I think this makes the whole feature request invalid, wdyt?
(
context.setStorageStateon the other hand would be a valid feature request)