chore: pretty print storage state (#5830)

This commit is contained in:
Pavel Feldman 2021-03-16 10:03:09 +08:00 committed by GitHub
parent c2db8da4df
commit 5cf13612cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 3 deletions

View file

@ -232,7 +232,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
const state = await channel.storageState(); const state = await channel.storageState();
if (options.path) { if (options.path) {
await mkdirIfNeeded(options.path); await mkdirIfNeeded(options.path);
await fsWriteFileAsync(options.path, JSON.stringify(state), 'utf8'); await fsWriteFileAsync(options.path, JSON.stringify(state, undefined, 2), 'utf8');
} }
return state; return state;
}); });

View file

@ -314,13 +314,14 @@ export abstract class BrowserContext extends SdkObject {
}); });
for (const origin of this._origins) { for (const origin of this._origins) {
const originStorage: types.OriginStorage = { origin, localStorage: [] }; const originStorage: types.OriginStorage = { origin, localStorage: [] };
result.origins.push(originStorage);
const frame = page.mainFrame(); const frame = page.mainFrame();
await frame.goto(internalMetadata, origin); await frame.goto(internalMetadata, origin);
const storage = await frame._evaluateExpression(`({ const storage = await frame._evaluateExpression(`({
localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name) })), localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name) })),
})`, false, undefined, 'utility'); })`, false, undefined, 'utility');
originStorage.localStorage = storage.localStorage; originStorage.localStorage = storage.localStorage;
if (storage.localStorage.length)
result.origins.push(originStorage);
} }
await page.close(internalMetadata); await page.close(internalMetadata);
} }

View file

@ -86,7 +86,7 @@ it('should round-trip through the file', async ({ browser, context, testInfo })
const path = testInfo.outputPath('storage-state.json'); const path = testInfo.outputPath('storage-state.json');
const state = await context.storageState({ path }); const state = await context.storageState({ path });
const written = await fs.promises.readFile(path, 'utf8'); const written = await fs.promises.readFile(path, 'utf8');
expect(JSON.stringify(state)).toBe(written); expect(JSON.stringify(state, undefined, 2)).toBe(written);
const context2 = await browser.newContext({ storageState: path }); const context2 = await browser.newContext({ storageState: path });
const page2 = await context2.newPage(); const page2 = await context2.newPage();