Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/PageCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class PageCollector<T> {
collector: (item: T) => void,
) => ListenerMap<PageEvents>;
#listeners = new WeakMap<Page, ListenerMap>();
#maxNavigationSaved = 3;
protected maxNavigationSaved = 3;

/**
* This maps a Page to a list of navigations with a sub-list
Expand Down Expand Up @@ -159,7 +159,7 @@ export class PageCollector<T> {
}
// Add the latest navigation first
navigations.unshift([]);
navigations.splice(this.#maxNavigationSaved);
navigations.splice(this.maxNavigationSaved);
}

protected cleanupPageDestroyed(page: Page) {
Expand All @@ -183,7 +183,7 @@ export class PageCollector<T> {
}

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]);
}
Expand Down Expand Up @@ -409,5 +409,6 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
} else {
navigations.unshift([]);
}
navigations.splice(this.maxNavigationSaved);
}
}
23 changes: 23 additions & 0 deletions tests/PageCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down