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> {
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
const waiter = Waiter.createForEvent(this, event);
if (logLine)
waiter.log(logLine);
waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
if (event !== Events.Page.Crash)
waiter.rejectOnEvent(this, Events.Page.Crash, new Error('Page crashed'));
if (event !== Events.Page.Close)
waiter.rejectOnEvent(this, Events.Page.Close, new Error('Page closed'));
const result = await waiter.waitForEvent(this, event, predicate as any);
waiter.dispose();
return result;
return this._wrapApiCall(async () => {
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
const waiter = Waiter.createForEvent(this, event);
if (logLine)
waiter.log(logLine);
waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
if (event !== Events.Page.Crash)
waiter.rejectOnEvent(this, Events.Page.Crash, new Error('Page crashed'));
if (event !== Events.Page.Close)
waiter.rejectOnEvent(this, Events.Page.Close, new Error('Page closed'));
const result = await waiter.waitForEvent(this, event, predicate as any);
waiter.dispose();
return result;
});
}
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);
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
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 }) => {

View File

@ -39,8 +39,11 @@ it('should respect timeout', async ({ page, playwright }) => {
it('should respect default timeout', async ({ page, playwright }) => {
let error = null;
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);
// 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 }) => {