Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(storage): keep falsy IndexedDB values and keys in storageState (#43429)
Yury Semikhatsky4cf54cbea772
diff --git a/packages/injected/src/storageScript.ts b/packages/injected/src/storageScript.tsindex efb3054cef..1426e6b01a 100644--- a/packages/injected/src/storageScript.ts+++ b/packages/injected/src/storageScript.ts@@ -69,7 +69,7 @@ export class StorageScript {this._isPlainObject(v)|| Array.isArray(v)|| typeof v === 'string'- || typeof v === 'number'+ || (typeof v === 'number' && Number.isFinite(v))|| typeof v === 'boolean'|| Object.is(v, null));@@ -105,7 +105,7 @@ export class StorageScript {if (objectStore.keyPath === null) {const { encoded, trivial } = this._trySerialize(key);- if (trivial)+ if (encoded === undefined)record.key = trivial;elserecord.keyEncoded = encoded;@@ -113,7 +113,7 @@ export class StorageScript {const value = await this._idbRequestToPromise(objectStore.get(key));const { encoded, trivial } = this._trySerialize(value);- if (trivial)+ if (encoded === undefined)record.value = trivial;elserecord.valueEncoded = encoded;@@ -215,8 +215,8 @@ export class StorageScript {await Promise.all(store.records.map(async record => {await this._idbRequestToPromise(objectStore.add(- record.value ?? parseEvaluationResultValue(record.valueEncoded),- record.key ?? parseEvaluationResultValue(record.keyEncoded),+ 'value' in record ? record.value : parseEvaluationResultValue(record.valueEncoded),+ 'key' in record ? record.key : parseEvaluationResultValue(record.keyEncoded),));}));diff --git a/tests/library/browsercontext-storage-state.spec.ts b/tests/library/browsercontext-storage-state.spec.tsindex d64808bd2e..9ee305ae64 100644--- a/tests/library/browsercontext-storage-state.spec.ts+++ b/tests/library/browsercontext-storage-state.spec.ts@@ -649,7 +649,12 @@ it('should support IndexedDB', async ({ page, server, contextFactory }) => {});for (const restore of ['newContext', 'setStorageState'] as const) {- it(`should roundtrip IndexedDB Map and Set with ${restore}`, { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42703' } }, async ({ page, server, contextFactory }, testInfo) => {+ it(`should roundtrip IndexedDB values and keys with ${restore}`, {+ annotation: [+ { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42703' },+ { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43418' },+ ],+ }, async ({ page, server, contextFactory }, testInfo) => {await page.goto(server.EMPTY_PAGE);await page.evaluate(() => new Promise<void>((resolve, reject) => {const request = indexedDB.open('collections', 1);@@ -661,6 +666,13 @@ for (const restore of ['newContext', 'setStorageState'] as const) {const store = transaction.objectStore('store');store.put(new Map([['mk', 'mv']]), 'map');store.put(new Set([1, 2]), 'set');+ store.put(0, 'zero');+ store.put(false, 'false');+ store.put('', 'empty');+ store.put(null, 'null');+ store.put(Infinity, 'infinity');+ store.put('first', 0);+ store.put('second', '');transaction.oncomplete = () => {db.close();resolve();@@ -678,7 +690,7 @@ for (const restore of ['newContext', 'setStorageState'] as const) {const restoredPage = await context.newPage();await restoredPage.goto(server.EMPTY_PAGE);- const values = await restoredPage.evaluate(async () => {+ const entries = await restoredPage.evaluate(async () => {const db = await new Promise<IDBDatabase>((resolve, reject) => {const request = indexedDB.open('collections', 1);request.onerror = () => reject(request.error);@@ -687,24 +699,30 @@ for (const restore of ['newContext', 'setStorageState'] as const) {const transaction = db.transaction('store', 'readonly');const store = transaction.objectStore('store');transaction.oncomplete = () => db.close();- const [map, set] = await Promise.all(['map', 'set'].map(key => new Promise<any>((resolve, reject) => {- const request = store.get(key);+ const [keys, values] = await Promise.all([store.getAllKeys(), store.getAll()].map(request => new Promise<any[]>((resolve, reject) => {request.onsuccess = () => resolve(request.result);request.onerror = () => reject(request.error);})));- return {- isMap: map instanceof Map,- map: [...map],- isSet: set instanceof Set,- set: [...set],- };- });- expect(values).toEqual({- isMap: true,- map: [['mk', 'mv']],- isSet: true,- set: [1, 2],+ return keys.map((key, i) => {+ const value = values[i];+ if (value instanceof Map)+ return [key, { map: [...value] }];+ if (value instanceof Set)+ return [key, { set: [...value] }];+ return [key, value];+ });});+ expect(entries).toEqual([+ [0, 'first'],+ ['', 'second'],+ ['empty', ''],+ ['false', false],+ ['infinity', Infinity],+ ['map', { map: [['mk', 'mv']] }],+ ['null', null],+ ['set', { set: [1, 2] }],+ ['zero', 0],+ ]);});}