Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Dmitry Gozman8e75dbffaa1e
diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.ts
index e587548933..f3e143ab6b 100644
--- a/packages/playwright-core/src/client/page.ts
+++ b/packages/playwright-core/src/client/page.ts
@@ -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> {
diff --git a/tests/page/page-wait-for-request.spec.ts b/tests/page/page-wait-for-request.spec.ts
index 91dc592b77..f9352bc55e 100644
--- a/tests/page/page-wait-for-request.spec.ts
+++ b/tests/page/page-wait-for-request.spec.ts
@@ -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 }) => {
diff --git a/tests/page/page-wait-for-response.spec.ts b/tests/page/page-wait-for-response.spec.ts
index 0fcc2e161f..3f346fd384 100644
--- a/tests/page/page-wait-for-response.spec.ts
+++ b/tests/page/page-wait-for-response.spec.ts
@@ -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 }) => {