diff --git a/src/PageCollector.ts b/src/PageCollector.ts index eeb29a209..8a419fc62 100644 --- a/src/PageCollector.ts +++ b/src/PageCollector.ts @@ -62,7 +62,7 @@ export class PageCollector { collector: (item: T) => void, ) => ListenerMap; #listeners = new WeakMap(); - #maxNavigationSaved = 3; + protected maxNavigationSaved = 3; /** * This maps a Page to a list of navigations with a sub-list @@ -159,7 +159,7 @@ export class PageCollector { } // Add the latest navigation first navigations.unshift([]); - navigations.splice(this.#maxNavigationSaved); + navigations.splice(this.maxNavigationSaved); } protected cleanupPageDestroyed(page: Page) { @@ -183,7 +183,7 @@ export class PageCollector { } const data: T[] = []; - for (let index = this.#maxNavigationSaved; index >= 0; index--) { + for (let index = this.maxNavigationSaved; index >= 0; index--) { if (navigations[index]) { data.push(...navigations[index]); } @@ -409,5 +409,6 @@ export class NetworkCollector extends PageCollector { } else { navigations.unshift([]); } + navigations.splice(this.maxNavigationSaved); } } diff --git a/tests/PageCollector.test.ts b/tests/PageCollector.test.ts index 6a8155fcc..6814002a5 100644 --- a/tests/PageCollector.test.ts +++ b/tests/PageCollector.test.ts @@ -284,6 +284,29 @@ describe('NetworkCollector', () => { page.emit('request', request); assert.equal(collector.getData(page, true).length, 3); }); + + it('should not grow beyond maxNavigationSaved', async () => { + const browser = getMockBrowser(); + const page = (await browser.pages())[0]; + const mainFrame = page.mainFrame(); + const collector = new NetworkCollector(browser); + await collector.init([page]); + + // Simulate 5 navigations (maxNavigationSaved is 3) + for (let i = 0; i < 5; i++) { + const req = getMockRequest({ + url: `http://example.com/nav${i}`, + navigationRequest: true, + frame: mainFrame, + }); + page.emit('request', req); + page.emit('framenavigated', mainFrame); + } + + // We expect 3 arrays in navigations (current + 2 saved) + // Each navigation has 1 request, so total should be 3 + assert.equal(collector.getData(page, true).length, 3); + }); }); describe('ConsoleCollector', () => {