Skip to content

Commit 9fe9ae4

Browse files
committed
fix(core): don't lose old user-profile storage
Closes #342
1 parent 98eabd1 commit 9fe9ae4

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

core/lib/UserProfile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export default class UserProfile {
1212
public static async export(session: Session) {
1313
const cookies = await session.browserContext.getCookies();
1414

15-
const storage: IDomStorage = {};
15+
// start with previous storage
16+
const storage: IDomStorage = session.options.userProfile?.storage ?? {};
17+
1618
for (const tab of session.tabsById.values()) {
1719
const page = tab.puppetPage;
1820

core/test/user-profile.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,61 @@ document.querySelector('#session').innerHTML = [session1,session2,session3].join
218218
await tab2.close();
219219
});
220220

221+
it("should keep profile information for sites that aren't loaded in a session", async () => {
222+
const meta = await connection.createSession({
223+
userProfile: {
224+
cookies: [],
225+
storage: {
226+
[koaServer.baseUrl]: {
227+
indexedDB: [],
228+
localStorage: [
229+
['Test1', 'value0'],
230+
['test2', 'value1'],
231+
],
232+
sessionStorage: [],
233+
},
234+
'https://previousSite.org': {
235+
indexedDB: [],
236+
localStorage: [['test', 'site1.org']],
237+
sessionStorage: [],
238+
},
239+
'https://site2.org': {
240+
indexedDB: [],
241+
localStorage: [['test2', 'site2.org']],
242+
sessionStorage: [],
243+
},
244+
},
245+
},
246+
});
247+
const tab = Session.getTab(meta);
248+
Helpers.needsClosing.push(tab.session);
249+
250+
koaServer.get('/unloaded', ctx => {
251+
ctx.body = `<body>
252+
<h1>storage page</h1>
253+
<script>
254+
localStorage.setItem('Test1', 'value1');
255+
</script>
256+
</body>`;
257+
});
258+
259+
await tab.goto(`${koaServer.baseUrl}/unloaded`);
260+
await tab.waitForLoad('PaintingStable');
261+
262+
const profile = await tab.session.exportUserProfile();
263+
expect(profile.cookies).toHaveLength(0);
264+
expect(profile.storage[koaServer.baseUrl]?.localStorage).toHaveLength(2);
265+
expect(profile.storage[koaServer.baseUrl]?.localStorage.find(x => x[0] === 'Test1')).toEqual([
266+
'Test1',
267+
'value1',
268+
]);
269+
expect(profile.storage['https://previousSite.org'].localStorage).toEqual([
270+
['test', 'site1.org'],
271+
]);
272+
expect(profile.storage['https://site2.org'].localStorage).toEqual([['test2', 'site2.org']]);
273+
await tab.close();
274+
});
275+
221276
it('should not make requests to end sites during profile "install"', async () => {
222277
const mitmSpy = jest.spyOn(HttpRequestHandler, 'onRequest');
223278
await connection.createSession({

0 commit comments

Comments
 (0)