Skip to content

Commit 4c65f59

Browse files
fix: retrieve data correctly with fewer than 3 navigations (#451)
Also adds test for navigation
1 parent 4a897f1 commit 4c65f59

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

src/PageCollector.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ export class PageCollector<T> {
144144
}
145145

146146
const data: T[] = [];
147+
147148
for (let index = this.#maxNavigationSaved; index >= 0; index--) {
148-
data.push(...navigations[index]);
149+
if (navigations[index]) {
150+
data.push(...navigations[index]);
151+
}
149152
}
150153
return data;
151154
}

tests/PageCollector.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,36 @@ describe('NetworkCollector', () => {
286286
page.emit('request', request);
287287
assert.equal(collector.getData(page).length, 2);
288288
});
289+
290+
it('works with previous navigations', async () => {
291+
const browser = getMockBrowser();
292+
const page = (await browser.pages())[0];
293+
const mainFrame = page.mainFrame();
294+
const navRequest = getMockRequest({
295+
navigationRequest: true,
296+
frame: page.mainFrame(),
297+
});
298+
const navRequest2 = getMockRequest({
299+
navigationRequest: true,
300+
frame: page.mainFrame(),
301+
});
302+
const request = getMockRequest();
303+
304+
const collector = new NetworkCollector(browser);
305+
await collector.init();
306+
page.emit('request', navRequest);
307+
assert.equal(collector.getData(page, true).length, 1);
308+
309+
page.emit('framenavigated', mainFrame);
310+
assert.equal(collector.getData(page, true).length, 1);
311+
312+
page.emit('request', navRequest2);
313+
assert.equal(collector.getData(page, true).length, 2);
314+
315+
page.emit('framenavigated', mainFrame);
316+
assert.equal(collector.getData(page, true).length, 2);
317+
318+
page.emit('request', request);
319+
assert.equal(collector.getData(page, true).length, 3);
320+
});
289321
});

tests/tools/network.test.js.snapshot

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ reqid=1 GET http://localhost:<port>/one [success - 200]
3434
reqid=2 GET http://localhost:<port>/two [success - 200]
3535
reqid=3 GET http://localhost:<port>/three [success - 200]
3636
`;
37+
38+
exports[`network > network_list_requests > list requests from previous navigations from redirects 1`] = `
39+
# list_request response
40+
## Network requests
41+
Showing 1-3 of 3 (Page 1 of 1).
42+
reqid=1 GET http://localhost:<port>/redirect [failed - 302]
43+
reqid=2 GET http://localhost:<port>/redirected [success - 200]
44+
reqid=3 GET http://localhost:<port>/redirected-page [success - 200]
45+
`;

tests/tools/network.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ describe('network', () => {
7171
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
7272
});
7373
});
74+
75+
it('list requests from previous navigations from redirects', async t => {
76+
server.addRoute('/redirect', async (_req, res) => {
77+
res.writeHead(302, {
78+
Location: server.getRoute('/redirected'),
79+
});
80+
res.end();
81+
});
82+
83+
server.addHtmlRoute(
84+
'/redirected',
85+
html`<script>
86+
document.location.href = '/redirected-page';
87+
</script>`,
88+
);
89+
90+
server.addHtmlRoute(
91+
'/redirected-page',
92+
html`<main>I was redirected 2 times</main>`,
93+
);
94+
95+
await withBrowser(async (response, context) => {
96+
await context.setUpNetworkCollectorForTesting();
97+
const page = context.getSelectedPage();
98+
await page.goto(server.getRoute('/redirect'));
99+
await listNetworkRequests.handler(
100+
{
101+
params: {
102+
includePreviousNavigations: true,
103+
},
104+
},
105+
response,
106+
context,
107+
);
108+
const responseData = await response.handle('list_request', context);
109+
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
110+
});
111+
});
74112
});
75113
describe('network_get_request', () => {
76114
it('attaches request', async () => {

tests/tools/script.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ describe('script', () => {
161161
'/iframe',
162162
html`<main><button>I am iframe button</button></main>`,
163163
);
164-
server.addRoute('/main', async (_req, res) => {
165-
res.write(html`<iframe src="/iframe"></iframe>`);
166-
res.end();
167-
});
164+
server.addHtmlRoute('/main', html`<iframe src="/iframe"></iframe>`);
168165

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

0 commit comments

Comments
 (0)