This example shows how to use Playwright's new browserContext.storageState()
and corresponding browser.newContext({storageState})
functionality.
This repo is appropriate for custom/advanced state storage (e.g. state merging). If you only need basic state storage, check out the docs for Reuse Authentication State.
This example uses features that are currently only available on the master
branch of Playwright.
For this code to work, you must manually install and build playwright in the playwright
directory. E.g:
git clone https://github.com/microsoft/playwright
cd playwright
npm install
npm run build
Every time you run the script, you should notice the visit counter increasing. This is stored in LocalStorage by the page the Playwright accesses.
Rinogo:playwright-state-example rinogo$ node example.js
You have been here...
LocalStorage: 10 time(s).
Rinogo:playwright-state-example rinogo$ node example.js
You have been here...
LocalStorage: 11 time(s).
Rinogo:playwright-state-example rinogo$ node example.js
You have been here...
LocalStorage: 12 time(s).
You can clear the saved state in the savedState
file to see the counter reset:
Rinogo:playwright-state-example rinogo$ rm savedState
Rinogo:playwright-state-example rinogo$ node example.js
You have been here...
LocalStorage: 1 time(s).
That's it. Thanks to the Playwright team for such a sweet tool. Now, go build something!