Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion src/PageCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ export class PageCollector<T> {
}

const data: T[] = [];
for (let index = this.#maxNavigationSaved; index >= 0; index--) {
const navigationNum = Math.min(
navigations.length - 1,
this.#maxNavigationSaved,
);
for (let index = navigationNum; index >= 0; index--) {
data.push(...navigations[index]);
}
return data;
Expand Down
32 changes: 32 additions & 0 deletions tests/PageCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,36 @@ describe('NetworkCollector', () => {
page.emit('request', request);
assert.equal(collector.getData(page).length, 2);
});

it('works with previous navigations', async () => {
const browser = getMockBrowser();
const page = (await browser.pages())[0];
const mainFrame = page.mainFrame();
const navRequest = getMockRequest({
navigationRequest: true,
frame: page.mainFrame(),
});
const navRequest2 = getMockRequest({
navigationRequest: true,
frame: page.mainFrame(),
});
const request = getMockRequest();

const collector = new NetworkCollector(browser);
await collector.init();
page.emit('request', navRequest);
assert.equal(collector.getData(page, true).length, 1);

page.emit('framenavigated', mainFrame);
assert.equal(collector.getData(page, true).length, 1);

page.emit('request', navRequest2);
assert.equal(collector.getData(page, true).length, 2);

page.emit('framenavigated', mainFrame);
assert.equal(collector.getData(page, true).length, 2);

page.emit('request', request);
assert.equal(collector.getData(page, true).length, 3);
});
});
9 changes: 9 additions & 0 deletions tests/tools/network.test.js.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ reqid=1 GET http://localhost:<port>/one [success - 200]
reqid=2 GET http://localhost:<port>/two [success - 200]
reqid=3 GET http://localhost:<port>/three [success - 200]
`;

exports[`network > network_list_requests > list requests from previous navigations from redirects 1`] = `
# list_request response
## Network requests
Showing 1-3 of 3 (Page 1 of 1).
reqid=1 GET http://localhost:<port>/redirect [failed - 302]
reqid=2 GET http://localhost:<port>/redirected [success - 200]
reqid=3 GET http://localhost:<port>/redirected-page [success - 200]
`;
38 changes: 38 additions & 0 deletions tests/tools/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ describe('network', () => {
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
});
});

it('list requests from previous navigations from redirects', async t => {
server.addRoute('/redirect', async (_req, res) => {
res.writeHead(302, {
Location: server.getRoute('/redirected'),
});
res.end();
});

server.addHtmlRoute(
'/redirected',
html`<script>
document.location.href = '/redirected-page';
</script>`,
);

server.addHtmlRoute(
'/redirected-page',
html`<main>I was redirected 2 times</main>`,
);

await withBrowser(async (response, context) => {
await context.setUpNetworkCollectorForTesting();
const page = context.getSelectedPage();
await page.goto(server.getRoute('/redirect'));
await listNetworkRequests.handler(
{
params: {
includePreviousNavigations: true,
},
},
response,
context,
);
const responseData = await response.handle('list_request', context);
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
});
});
});
describe('network_get_request', () => {
it('attaches request', async () => {
Expand Down
5 changes: 1 addition & 4 deletions tests/tools/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ describe('script', () => {
'/iframe',
html`<main><button>I am iframe button</button></main>`,
);
server.addRoute('/main', async (_req, res) => {
res.write(html`<iframe src="/iframe"></iframe>`);
res.end();
});
server.addHtmlRoute('/main', html`<iframe src="/iframe"></iframe>`);

await withBrowser(async (response, context) => {
const page = context.getSelectedPage();
Expand Down