fix(errors): waitForEvent/Request/Response should point to the api call (#11229)

This commit is contained in:
Dmitry Gozman 2022-01-06 14:47:52 -08:00 committed by GitHub
parent d629fe57ab
commit 8e75dbffaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -382,19 +382,21 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
} }
private async _waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions, logLine?: string): Promise<any> { private async _waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions, logLine?: string): Promise<any> {
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate); return this._wrapApiCall(async () => {
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate; const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
const waiter = Waiter.createForEvent(this, event); const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
if (logLine) const waiter = Waiter.createForEvent(this, event);
waiter.log(logLine); if (logLine)
waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`); waiter.log(logLine);
if (event !== Events.Page.Crash) waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
waiter.rejectOnEvent(this, Events.Page.Crash, new Error('Page crashed')); if (event !== Events.Page.Crash)
if (event !== Events.Page.Close) waiter.rejectOnEvent(this, Events.Page.Crash, new Error('Page crashed'));
waiter.rejectOnEvent(this, Events.Page.Close, new Error('Page closed')); if (event !== Events.Page.Close)
const result = await waiter.waitForEvent(this, event, predicate as any); waiter.rejectOnEvent(this, Events.Page.Close, new Error('Page closed'));
waiter.dispose(); const result = await waiter.waitForEvent(this, event, predicate as any);
return result; waiter.dispose();
return result;
});
} }
async goBack(options: channels.PageGoBackOptions = {}): Promise<Response | null> { async goBack(options: channels.PageGoBackOptions = {}): Promise<Response | null> {

View File

@ -49,6 +49,9 @@ it('should respect timeout', async ({ page, playwright }) => {
await page.waitForEvent('request', { predicate: () => false, timeout: 1 }).catch(e => error = e); await page.waitForEvent('request', { predicate: () => false, timeout: 1 }).catch(e => error = e);
expect(error).toBeInstanceOf(playwright.errors.TimeoutError); expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
expect(error.message).toContain('Timeout 1ms exceeded while waiting for event "request"'); expect(error.message).toContain('Timeout 1ms exceeded while waiting for event "request"');
// Error stack should point to the api call.
const firstFrame = error.stack.split('\n').find(line => line.startsWith(' at '));
expect(firstFrame).toContain(__filename);
}); });
it('should respect default timeout', async ({ page, playwright }) => { it('should respect default timeout', async ({ page, playwright }) => {

View File

@ -39,8 +39,11 @@ it('should respect timeout', async ({ page, playwright }) => {
it('should respect default timeout', async ({ page, playwright }) => { it('should respect default timeout', async ({ page, playwright }) => {
let error = null; let error = null;
page.setDefaultTimeout(1); page.setDefaultTimeout(1);
await page.waitForEvent('response', () => false).catch(e => error = e); await page.waitForResponse(() => false).catch(e => error = e);
expect(error).toBeInstanceOf(playwright.errors.TimeoutError); expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
// Error stack should point to the api call.
const firstFrame = error.stack.split('\n').find(line => line.startsWith(' at '));
expect(firstFrame).toContain(__filename);
}); });
it('should log the url', async ({ page }) => { it('should log the url', async ({ page }) => {