Skip to content

Commit

Permalink
chore: pretty print storage state (#5830)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 16, 2021
1 parent c2db8da commit 5cf1361
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
const state = await channel.storageState();
if (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;
});
Expand Down
3 changes: 2 additions & 1 deletion src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,14 @@ export abstract class BrowserContext extends SdkObject {
});
for (const origin of this._origins) {
const originStorage: types.OriginStorage = { origin, localStorage: [] };
result.origins.push(originStorage);
const frame = page.mainFrame();
await frame.goto(internalMetadata, origin);
const storage = await frame._evaluateExpression(`({
localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name) })),
})`, false, undefined, 'utility');
originStorage.localStorage = storage.localStorage;
if (storage.localStorage.length)
result.origins.push(originStorage);
}
await page.close(internalMetadata);
}
Expand Down
2 changes: 1 addition & 1 deletion test/browsercontext-storage-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ it('should round-trip through the file', async ({ browser, context, testInfo })
const path = testInfo.outputPath('storage-state.json');
const state = await context.storageState({ path });
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 page2 = await context2.newPage();
Expand Down

0 comments on commit 5cf1361

Please sign in to comment.