Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Setting sessionStorage in a Playwright test throws a SecurityError

Your error

page.evaluate: SecurityError: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.1 more line it can show

Fix it fast

Most common cause, 1 of 3

The test writes sessionStorage before the page has navigated

Open a page of your app first so the document gets your site's origin, write the values, then reload so the app reads them on start-up.

Fix
await page.goto('https://example.com/');
await page.evaluate(values => {
  for (const [key, value] of Object.entries(values)) {
    window.sessionStorage.setItem(key, value);
  }
}, { token: 'abc123' });
await page.reload();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Playwright throws SecurityError: Access is denied for this document when sessionStorage is touched on a page that is still at about:blank. That document has an opaque origin, so the browser blocks storage. Call page.goto on your app first and then setItem, or use context.addInitScript with a hostname check. storageState never restores sessionStorage. Applies to Playwright 1.65.

  • Applies to Playwright 1.65.1 (@playwright/test), Chromium build bundled with Playwright 1.65.1 (error wording)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the page URL right before the line that throws. about:blank there means cause 1. If the error only appears as a page error from an init script, it is cause 2; no error but missing values is cause 3.

Run this first
console.log('url before sessionStorage:', page.url());

Then find the line you see in this list and open its fix:

  1. 1The test writes sessionStorage before the page has navigatedpage.url() prints about:blank on the line that calls sessionStorage.setItem
  2. 2An init script sets sessionStorage in every document, blank ones includedThe SecurityError shows up as a page error (pageerror or weberror) thrown from the addInitScript function, even though the values do reach your site
  3. 3storageState is expected to bring sessionStorage backNo error at all, but values written in a setup project are gone in the next test

What does the error look like?

  • page.evaluate: SecurityError: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.
  • Session storage is empty after loading storageState from playwright/.auth

Cause 1 of 3

The test writes sessionStorage before the page has navigated

You seepage.url() prints about:blank on the line that calls sessionStorage.setItem

Check

A new Playwright page starts on about:blank, and that blank document has an opaque origin. The HTML standard makes the sessionStorage getter throw a SecurityError for an opaque origin, and Chromium phrases it as Access is denied for this document. Storage only exists once the page belongs to a real scheme, host and port.

Check
const page = await context.newPage();
console.log(page.url()); // about:blank

Fix

Open a page of your app first so the document gets your site's origin, write the values, then reload so the app reads them on start-up.

Fix
await page.goto('https://example.com/');
await page.evaluate(values => {
  for (const [key, value] of Object.entries(values)) {
    window.sessionStorage.setItem(key, value);
  }
}, { token: 'abc123' });
await page.reload();

Verify

The evaluate call returns your value instead of throwing.

Verify
console.log(await page.evaluate(() => sessionStorage.getItem('token')));

Confirmed by html.spec.whatwg.org

Did this fix it?

Cause 2 of 3

An init script sets sessionStorage in every document, blank ones included

You seeThe SecurityError shows up as a page error (pageerror or weberror) thrown from the addInitScript function, even though the values do reach your site

Check

context.addInitScript runs its script whenever a page is created or navigated and whenever a child frame is attached or navigated. Its first run happens in the blank start page, and later runs can land in frames without a usable origin, so an unguarded setItem throws there. The throw surfaces as an uncaught page error, which clutters logs and fails suites that treat page errors as test failures.

Check
grep -rn -A6 "addInitScript" tests/

Fix

Wrap the writes in a hostname check, the same way the Playwright authentication guide does, so the script only touches storage on your own site.

Fix
await context.addInitScript(values => {
  if (window.location.hostname === 'example.com') {
    for (const [key, value] of Object.entries(values))
      window.sessionStorage.setItem(key, value);
  }
}, savedValues);
await page.goto('https://example.com/');

Verify

The page opens on your site with the values already present, and no SecurityError appears in the test output.

Verify
npx playwright test --reporter=list

Confirmed by playwright.dev

Did this fix it?

Cause 3 of 3

storageState is expected to bring sessionStorage back

You seeNo error at all, but values written in a setup project are gone in the next test

Check

Playwright's storage state holds cookies, local storage, IndexedDB, origin private file system entries and virtual WebAuthn credentials. Session storage is not in that list, and the authentication guide says it is specific to one domain and not kept across page loads.

Check
node -e "console.log(Object.keys(require('./playwright/.auth/user.json')))"

Fix

Save sessionStorage to its own file at the end of the setup step and put it back with a guarded init script in the tests that need it.

Fix
// setup
const saved = await page.evaluate(() => JSON.stringify(sessionStorage));
fs.writeFileSync('playwright/.auth/session.json', saved, 'utf-8');
// test
const values = JSON.parse(fs.readFileSync('playwright/.auth/session.json', 'utf-8'));
await context.addInitScript(v => {
  if (window.location.hostname === 'example.com')
    for (const [key, value] of Object.entries(v)) window.sessionStorage.setItem(key, value);
}, values);

Verify

A test that starts from the saved files lands on a signed-in page without logging in again.

Verify
npx playwright test tests/dashboard.spec.ts

Confirmed by playwright.dev

Did this fix it?

How do I stop it coming back?

  • Put the first page.goto in a fixture or beforeEach, so no test can reach storage while still on about:blank.
  • Give every addInitScript that writes storage a hostname guard, and keep the hostname in one config value.
  • Keep session-only data in its own saved file next to the storageState file, and document which one each project loads.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.