Learning center

Capture and seed the local storage or IndexedDB value that changed the behavior

A value written last week by an older version of the app can make today's page fail. The maintainer's browser does not have that value, so the steps alone will not reproduce it. Find the value, send it as data, and have the maintainer write it before the page's scripts run.

Find the value#

Start from the difference between a browser that fails and one that does not.

  1. In DevTools' Application panel, open Local storage, Session storage and IndexedDB for the page's origin.

  2. Copy all keys into a file. For local storage, the Console is quicker:

    copy(JSON.stringify(Object.fromEntries(Object.entries(localStorage)), null, 2))

    copy() is a DevTools Console utility that puts its argument on the clipboard. Console utilities API

  3. Remove keys one at a time, reloading after each, until the bug stops. The last key you removed is the one it needs. Halve the set each round if there are many keys.

Write down the key, the value and the code version that likely wrote it, if the value carries a schema version or a date.

Seed it before the page loads#

A value written after the page has started may come too late, since many apps read storage once at startup. Write it before any page script runs.

In Playwright, an init script runs in every new document before the page's own scripts:

await context.addInitScript(() => {
  if (location.origin === 'https://staging.example.test') {
    localStorage.setItem('cart', JSON.stringify({ schema: 2, items: [{ sku: 'A-1', qty: 1 }] }));
  }
});
await page.goto('https://staging.example.test/cart');

Playwright: addInitScript

An init script runs on every navigation, so the value is reset each time. That is usually what a reproduction wants. If the app is supposed to migrate the value and save it back, write it only once, guarded by a flag in sessionStorage.

Without Playwright, set the value in the Console on the right origin, then reload:

localStorage.setItem('cart', '{"schema":2,"items":[{"sku":"A-1","qty":1}]}'); location.reload();

IndexedDB needs a different approach#

IndexedDB is asynchronous and structured: databases, object stores, keys and indexes. Copying it by hand is error-prone.

  • Playwright's storage state includes IndexedDB when you ask for it with storageState({ indexedDB: true }). In our Playwright 1.63 fixture, a record we wrote was absent from the default snapshot and present with the option. Load the file with newContext({ storageState }). Replay a signed-in state covers the file.
  • By hand, describe the database name and version, the object store, the record's key and the record's value as JSON. The maintainer writes a short seeding script, or adds a fixture to the app's tests.

Record the database version. An app that upgrades its IndexedDB schema runs onupgradeneeded only when the stored version is lower, so a seeded database at the wrong version takes a different code path.

Keep personal data out of it#

Stored values can hold names, drafts, tokens and cached API responses. Replace personal values with synthetic ones that keep the same shape: the same length, character set, nesting and types. A value that fails because it has 51 items still fails with 51 synthetic items.

Worked example: a draft that crashed the editor#

This example is fictional. A writer reports that the editor goes blank when they open it. Clearing site data fixes it, which the writer does not want, because it would lose their draft.

The developer asks the writer to export local storage before clearing anything. The export shows editor.draft with a value whose blocks array includes {"type":"embed","url":null}. Seeding a synthetic draft with one null embed block before page load blanks the developer's editor too. The editor's embed renderer assumed a URL. The fix handled the null, and the reproduction became a regression test with the same seeded value.

Questions#

What about cookies?

Cookies travel to the server, so they can change the response rather than the page's behavior. Add them with context.addCookies() or a storage state file, and never copy a real session cookie.

Does an init script run in iframes?

Yes, Playwright runs it in child frames too. Guard it by location.origin, as in the example, so it writes only to the origin you mean.

The bug comes back after I clear storage. Is storage ruled out?

Not entirely. The app may write the bad value again during startup or from the server's response. Watch the storage after a clean load to see when the value appears.

Return to reproducing what someone else saw for the other layers a replay needs.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading