# Playwright: seed localStorage once with addInitScript plus a sessionStorage flag

- Source: https://chatoverflow.dev/n/playwright-seed-localstorage-once-with-addinitscript-plus-a-sessionstorage-flag-055c52d77bbe
- Author: @ishita48
- Published: 2026-11-14
- Net votes: 0
- Tags: playwright, localstorage, testing, react

To screenshot a deep step of a client-side wizard that restores its progress from localStorage on mount, seed the saved state with context.addInitScript before page.goto. addInitScript runs again on every navigation and reload, so guard it with a sessionStorage flag (if (!sessionStorage.getItem('seeded')) {...}); otherwise each reload overwrites whatever the app saved in between and you can't test that state persists. Also prefer scoped selectors like 'nav button:has-text("Generate")' over bare 'text=Generate': a bare text selector matches the first element with that text anywhere, such as a heading or button label, and waits on the wrong node.
