Skip to content

Commit c409f8c

Browse files
authored
test: do not keep evaluate promises pending across other calls (#43656)
1 parent 81a95be commit c409f8c

6 files changed

Lines changed: 51 additions & 57 deletions

File tree

‎tests/library/capabilities.spec.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ it('CA-011 should not crash on showDirectoryPicker', async ({ page, server, brow
132132
await page.goto(server.EMPTY_PAGE);
133133
// "User activation is required to show a file picker." - so we click first.
134134
await page.locator('body').click();
135-
page.evaluate(async () => {
136-
const dir = await (window as any).showDirectoryPicker();
137-
return dir.name;
138-
// In headless it throws (aborted), in headed it stalls (Test ended) and waits for the picker to be accepted.
139-
}).catch(e => expect(e.message).toMatch(/((DOMException|AbortError): .*The user aborted a request|Test ended)/));
135+
await page.evaluate(() => {
136+
(window as any).showDirectoryPicker().catch(e => { (window as any).__pickerError = String(e); });
137+
});
140138
// The dialog will not be accepted, so we just wait for some time to
141139
// to give the browser a chance to crash.
142140
await page.waitForTimeout(3_000);
141+
// In headless it throws (aborted), in headed it stalls and waits for the picker to be accepted.
142+
const error = await page.evaluate(() => (window as any).__pickerError ?? 'stalled');
143+
expect(error).toMatch(/((DOMException|AbortError): .*The user aborted a request|stalled)/);
143144
});
144145

145146
it('CA-012 should not crash on storage.getDirectory()', {

‎tests/library/tracing.spec.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -937,17 +937,15 @@ test('TR-041 should not emit after w/o before', async ({ browserType, mode }, te
937937
const page = await context.newPage();
938938

939939
await context.tracing.start({ name: 'name1', snapshots: true });
940-
const evaluatePromise = page.evaluate(() => {
941-
console.log('started');
942-
return new Promise(f => (window as any).callback = f);
943-
}).catch(() => {});
944-
await page.waitForEvent('console');
940+
// The wait spans the chunks: its "before" is in the first chunk, and its "after" must not get into the second one.
941+
const consolePromise = page.waitForEvent('console');
942+
await page.evaluate(() => 1);
945943
await context.tracing.stopChunk({ path: testInfo.outputPath('trace1.zip') });
946944
expect(fs.existsSync(path.join(tracesDir, 'name1.trace'))).toBe(true);
947945

948946
await context.tracing.startChunk({ name: 'name2' });
949-
await page.evaluateHandle(() => (window as any).callback());
950-
await evaluatePromise;
947+
await page.evaluate(() => console.log('started'));
948+
await consolePromise;
951949
await context.tracing.stop({ path: testInfo.outputPath('trace2.zip') });
952950
expect(fs.existsSync(path.join(tracesDir, 'name2.trace'))).toBe(true);
953951

@@ -972,19 +970,20 @@ test('TR-041 should not emit after w/o before', async ({ browserType, mode }, te
972970
{
973971
type: 'before',
974972
callId: expect.any(Number),
973+
title: 'Wait for event "console"',
975974
},
976975
{
977976
type: 'before',
978977
callId: expect.any(Number),
979-
title: 'Wait for event "console"',
980978
},
981979
{
982980
type: 'after',
983981
callId: expect.any(Number),
984982
},
985983
]);
986-
call1 = sanitized[0].callId;
984+
expect(sanitized[1].callId).toBeGreaterThan(sanitized[0].callId);
987985
expect(sanitized[1].callId).toBe(sanitized[2].callId);
986+
call1 = sanitized[1].callId;
988987
}
989988

990989
let call2before: number;

‎tests/page/page-drag.spec.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ it('DD-019 should handle custom dataTransfer', async ({ page, browserName, isWin
483483
it.fixme(browserName === 'webkit' && isWindows);
484484
await page.setContent(`<button draggable="true">Draggable</button>`);
485485

486-
const resultPromise = page.evaluate(() =>
487-
new Promise(resolve => {
486+
await page.evaluate(() => {
487+
(window as any).__result = new Promise(resolve => {
488488
document.addEventListener('dragstart', event => {
489489
event.dataTransfer!.setData('custom-type', 'Hello World');
490490
}, false);
@@ -503,15 +503,15 @@ it('DD-019 should handle custom dataTransfer', async ({ page, browserName, isWin
503503
data: event.dataTransfer!.getData('custom-type'),
504504
});
505505
}, false);
506-
})
507-
);
506+
});
507+
});
508508

509509
await page.hover('[draggable="true"]');
510510
await page.mouse.down();
511511
await page.mouse.move(100, 100);
512512
await page.mouse.up();
513513

514-
await expect(resultPromise).resolves.toEqual({
514+
expect(await page.evaluate(() => (window as any).__result)).toEqual({
515515
types: ['custom-type'],
516516
data: 'Hello World',
517517
});

‎tests/page/page-evaluate.spec.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,12 @@ it('EV-062 should simulate a user gesture', async ({ page }) => {
574574
});
575575

576576
it('EV-063 should throw a nice error after a navigation', async ({ page }) => {
577-
const errorPromise = page.evaluate(() => new Promise(f => window['__resolve'] = f)).catch(e => e) as Promise<Error>;
578-
await Promise.all([
579-
page.waitForNavigation(),
580-
page.evaluate(() => {
581-
window.location.reload();
582-
setTimeout(() => window['__resolve'](42), 1000);
583-
})
584-
]);
585-
const error = await errorPromise;
577+
const navigationPromise = page.waitForNavigation();
578+
const error = await page.evaluate(() => new Promise(f => setTimeout(() => {
579+
window.location.reload();
580+
setTimeout(() => f(42), 1000);
581+
}, 0))).catch(e => e) as Error;
582+
await navigationPromise;
586583
expect(error.message).toContain('navigation');
587584
});
588585

‎tests/page/page-request-fulfill.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ it('RO-100 should not throw if request was cancelled by the page', async ({ page
106106
const interceptPromise = new Promise<Route>(f => interceptCallback = f);
107107
await page.route('**/data.json', route => interceptCallback(route));
108108
await page.goto(server.EMPTY_PAGE);
109-
page.evaluate(url => {
109+
await page.evaluate(url => {
110110
globalThis.controller = new AbortController();
111-
return fetch(url, { signal: globalThis.controller.signal });
112-
}, server.PREFIX + '/data.json').catch(() => {});
111+
fetch(url, { signal: globalThis.controller.signal }).catch(() => {});
112+
}, server.PREFIX + '/data.json');
113113
const route = await interceptPromise;
114114
const failurePromise = page.waitForEvent('requestfailed');
115115
await page.evaluate(() => globalThis.controller.abort());

‎tests/page/page-route.spec.ts‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,48 +230,45 @@ it('RO-012 should pause intercepted XHR until continue', async ({ page, server,
230230
const routePromise = new Promise(r => resolveRoute = r);
231231
await page.route('**/global-var.html', async route => resolveRoute(route));
232232
let xhrFinished = false;
233-
const statusPromise = page.evaluate(async () => {
234-
const request = new XMLHttpRequest();
235-
request.open('GET', '/global-var.html', false); // `false` makes the request synchronous
236-
request.send(null);
237-
return request.status;
238-
}).then(r => {
239-
xhrFinished = true;
240-
return r;
233+
page.on('requestfinished', request => {
234+
if (request.url().endsWith('/global-var.html'))
235+
xhrFinished = true;
236+
});
237+
// The synchronous request blocks the page, so it is started from a timer.
238+
await page.evaluate(() => {
239+
(window as any).__statusPromise = new Promise(resolve => setTimeout(() => {
240+
const request = new XMLHttpRequest();
241+
request.open('GET', '/global-var.html', false); // `false` makes the request synchronous
242+
request.send(null);
243+
resolve(request.status);
244+
}, 0));
241245
});
242246
const route = await routePromise;
243247
// Check that intercepted request is actually paused.
244248
await new Promise(r => setTimeout(r, 500));
245249
expect(xhrFinished).toBe(false);
246-
const [status] = await Promise.all([
247-
statusPromise,
248-
(route as any).continue()
249-
]);
250-
expect(status).toBe(200);
250+
await (route as any).continue();
251+
expect(await page.evaluate(() => (window as any).__statusPromise)).toBe(200);
251252
});
252253

253254
it('RO-013 should pause intercepted fetch request until continue', async ({ page, server }) => {
254255
await page.goto(server.EMPTY_PAGE);
255256
let resolveRoute;
256257
const routePromise = new Promise(r => resolveRoute = r);
257258
await page.route('**/global-var.html', async route => resolveRoute(route));
258-
let fetchFinished = false;
259-
const statusPromise = page.evaluate(async () => {
260-
const response = await fetch('/global-var.html');
261-
return response.status;
262-
}).then(r => {
263-
fetchFinished = true;
264-
return r;
259+
await page.evaluate(() => {
260+
(window as any).__fetchFinished = false;
261+
(window as any).__statusPromise = fetch('/global-var.html').then(response => {
262+
(window as any).__fetchFinished = true;
263+
return response.status;
264+
});
265265
});
266266
const route = await routePromise;
267267
// Check that intercepted request is actually paused.
268268
await new Promise(r => setTimeout(r, 500));
269-
expect(fetchFinished).toBe(false);
270-
const [status] = await Promise.all([
271-
statusPromise,
272-
(route as any).continue()
273-
]);
274-
expect(status).toBe(200);
269+
expect(await page.evaluate(() => (window as any).__fetchFinished)).toBe(false);
270+
await (route as any).continue();
271+
expect(await page.evaluate(() => (window as any).__statusPromise)).toBe(200);
275272
});
276273

277274
it('RO-014 should work with custom referer headers', async ({ page, server, browserName, browserMajorVersion }) => {

0 commit comments

Comments
 (0)