diff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.md index 483b2b293b..ad5f7e7af3 100644 --- a/docs/src/api/class-browsercontext.md +++ b/docs/src/api/class-browsercontext.md @@ -239,12 +239,12 @@ Use [`method: Page.waitForLoadState`] to wait until the page gets to a particula cases). ::: -## event: BrowserContext.pageError +## event: BrowserContext.webError * since: v1.38 -- argument: <[PageError]> +- argument: <[WebError]> -Emitted when unhandled exceptions occur on any pages created through this -context. To only listen for `pageError` events from a particular page, use [`event: Page.pageError`]. +Emitted when exception is unhandled in any of the pages in this +context. To listen for errors from a particular page, use [`event: Page.pageError`] instead. ## event: BrowserContext.request * since: v1.12 diff --git a/docs/src/api/class-pageerror.md b/docs/src/api/class-weberror.md similarity index 57% rename from docs/src/api/class-pageerror.md rename to docs/src/api/class-weberror.md index 35632b6082..db56fdee0b 100644 --- a/docs/src/api/class-pageerror.md +++ b/docs/src/api/class-weberror.md @@ -1,13 +1,12 @@ -# class: PageError +# class: WebError * since: v1.38 -[PageError] class represents objects created by context when there are unhandled -execeptions thrown on the pages and dispatched via the [`event: BrowserContext.pageError`] event. +[WebError] class represents an unhandled exeception thrown in the page. It is dispatched via the [`event: BrowserContext.webError`] event. ```js // Log all uncaught errors to the terminal -context.on('pageerror', pageerror => { - console.log(`Uncaught exception: "${pageerror.error()}"`); +context.on('weberror', webError => { + console.log(`Uncaught exception: "${webError.error()}"`); }); // Navigate to a page with an exception. @@ -16,8 +15,8 @@ await page.goto('data:text/html,'); ```java // Log all uncaught errors to the terminal -context.onPageError(pagerror -> { - System.out.println("Uncaught exception: " + pagerror.error()); +context.onWebError(webError -> { + System.out.println("Uncaught exception: " + webError.error()); }); // Navigate to a page with an exception. @@ -26,7 +25,7 @@ page.navigate("data:text/html,"); ```python async # Log all uncaught errors to the terminal -context.on("pageerror", lambda pageerror: print(f"uncaught exception: {pageerror.error}")) +context.on("weberror", lambda web_error: print(f"uncaught exception: {web_error.error}")) # Navigate to a page with an exception. await page.goto("data:text/html,") @@ -34,7 +33,7 @@ await page.goto("data:text/html,") ```python sync # Log all uncaught errors to the terminal -context.on("pageerror", lambda pageerror: print(f"uncaught exception: {pageerror.error}")) +context.on("weberror", lambda web_error: print(f"uncaught exception: {web_error.error}")) # Navigate to a page with an exception. page.goto("data:text/html,") @@ -42,25 +41,25 @@ page.goto("data:text/html,") ```csharp // Log all uncaught errors to the terminal -context.PageError += (_, pageerror) => +context.WebError += (_, webError) => { - Console.WriteLine("Uncaught exception: " + pageerror.Error); + Console.WriteLine("Uncaught exception: " + webError.Error); }; ``` -## method: PageError.page +## method: WebError.page * since: v1.38 - returns: <[null]|[Page]> The page that produced this unhandled exception, if any. -## method: PageError.error +## method: WebError.error * since: v1.38 - returns: <[Error]> Unhandled error that was thrown. -## method: PageError.error +## method: WebError.error * since: v1.38 * langs: java, csharp - returns: <[string]> diff --git a/packages/playwright-core/src/client/api.ts b/packages/playwright-core/src/client/api.ts index 905c8543ce..03d54c92d7 100644 --- a/packages/playwright-core/src/client/api.ts +++ b/packages/playwright-core/src/client/api.ts @@ -42,4 +42,4 @@ export { Video } from './video'; export { Worker } from './worker'; export { CDPSession } from './cdpSession'; export { Playwright } from './playwright'; -export { PageError } from './pageError'; +export { WebError } from './webError'; diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts index 036f6a70e2..94f2a809b6 100644 --- a/packages/playwright-core/src/client/browserContext.ts +++ b/packages/playwright-core/src/client/browserContext.ts @@ -41,7 +41,7 @@ import { rewriteErrorMessage } from '../utils/stackTrace'; import { HarRouter } from './harRouter'; import { ConsoleMessage } from './consoleMessage'; import { Dialog } from './dialog'; -import { PageError } from './pageError'; +import { WebError } from './webError'; import { parseError } from '../protocol/serializers'; export class BrowserContext extends ChannelOwner implements api.BrowserContext { @@ -105,7 +105,7 @@ export class BrowserContext extends ChannelOwner this._channel.on('pageError', ({ error, page }) => { const pageObject = Page.from(page); const parsedError = parseError(error); - this.emit(Events.BrowserContext.PageError, new PageError(pageObject, parsedError)); + this.emit(Events.BrowserContext.WebError, new WebError(pageObject, parsedError)); if (pageObject) pageObject.emit(Events.Page.PageError, parsedError); }); diff --git a/packages/playwright-core/src/client/events.ts b/packages/playwright-core/src/client/events.ts index 72747e0f51..287e5915e5 100644 --- a/packages/playwright-core/src/client/events.ts +++ b/packages/playwright-core/src/client/events.ts @@ -41,7 +41,7 @@ export const Events = { Page: 'page', // Can't use just 'error' due to node.js special treatment of error events. // @see https://nodejs.org/api/events.html#events_error_events - PageError: 'pageerror', + WebError: 'weberror', BackgroundPage: 'backgroundpage', ServiceWorker: 'serviceworker', Request: 'request', diff --git a/packages/playwright-core/src/client/pageError.ts b/packages/playwright-core/src/client/webError.ts similarity index 94% rename from packages/playwright-core/src/client/pageError.ts rename to packages/playwright-core/src/client/webError.ts index 71b0b329c5..13a942abee 100644 --- a/packages/playwright-core/src/client/pageError.ts +++ b/packages/playwright-core/src/client/webError.ts @@ -17,7 +17,7 @@ import type * as api from '../../types/types'; import type { Page } from './page'; -export class PageError implements api.PageError { +export class WebError implements api.WebError { private _page: Page | null; private _error: Error; diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index aca17d5459..d8f5758d5c 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -7625,11 +7625,10 @@ export interface BrowserContext { on(event: 'page', listener: (page: Page) => void): this; /** - * Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError` - * events from a particular page, use - * [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error). + * Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular + * page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead. */ - on(event: 'pageerror', listener: (pageError: PageError) => void): this; + on(event: 'weberror', listener: (webError: WebError) => void): this; /** * Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To @@ -7704,7 +7703,7 @@ export interface BrowserContext { /** * Adds an event listener that will be automatically removed after it is triggered once. See `addListener` for more information about this event. */ - once(event: 'pageerror', listener: (pageError: PageError) => void): this; + once(event: 'weberror', listener: (webError: WebError) => void): this; /** * Adds an event listener that will be automatically removed after it is triggered once. See `addListener` for more information about this event. @@ -7818,11 +7817,10 @@ export interface BrowserContext { addListener(event: 'page', listener: (page: Page) => void): this; /** - * Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError` - * events from a particular page, use - * [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error). + * Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular + * page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead. */ - addListener(event: 'pageerror', listener: (pageError: PageError) => void): this; + addListener(event: 'weberror', listener: (webError: WebError) => void): this; /** * Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To @@ -7897,7 +7895,7 @@ export interface BrowserContext { /** * Removes an event listener added by `on` or `addListener`. */ - removeListener(event: 'pageerror', listener: (pageError: PageError) => void): this; + removeListener(event: 'weberror', listener: (webError: WebError) => void): this; /** * Removes an event listener added by `on` or `addListener`. @@ -7952,7 +7950,7 @@ export interface BrowserContext { /** * Removes an event listener added by `on` or `addListener`. */ - off(event: 'pageerror', listener: (pageError: PageError) => void): this; + off(event: 'weberror', listener: (webError: WebError) => void): this; /** * Removes an event listener added by `on` or `addListener`. @@ -8066,11 +8064,10 @@ export interface BrowserContext { prependListener(event: 'page', listener: (page: Page) => void): this; /** - * Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError` - * events from a particular page, use - * [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error). + * Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular + * page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead. */ - prependListener(event: 'pageerror', listener: (pageError: PageError) => void): this; + prependListener(event: 'weberror', listener: (webError: WebError) => void): this; /** * Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To @@ -8659,11 +8656,10 @@ export interface BrowserContext { waitForEvent(event: 'page', optionsOrPredicate?: { predicate?: (page: Page) => boolean | Promise, timeout?: number } | ((page: Page) => boolean | Promise)): Promise; /** - * Emitted when unhandled exceptions occur on any pages created through this context. To only listen for `pageError` - * events from a particular page, use - * [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error). + * Emitted when exception is unhandled in any of the pages in this context. To listen for errors from a particular + * page, use [page.on('pageerror')](https://playwright.dev/docs/api/class-page#page-event-page-error) instead. */ - waitForEvent(event: 'pageerror', optionsOrPredicate?: { predicate?: (pageError: PageError) => boolean | Promise, timeout?: number } | ((pageError: PageError) => boolean | Promise)): Promise; + waitForEvent(event: 'weberror', optionsOrPredicate?: { predicate?: (webError: WebError) => boolean | Promise, timeout?: number } | ((webError: WebError) => boolean | Promise)): Promise; /** * Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To @@ -17960,35 +17956,6 @@ export interface Mouse { wheel(deltaX: number, deltaY: number): Promise; } -/** - * {@link PageError} class represents objects created by context when there are unhandled execeptions thrown on the - * pages and dispatched via the - * [browserContext.on('pageerror')](https://playwright.dev/docs/api/class-browsercontext#browser-context-event-page-error) - * event. - * - * ```js - * // Log all uncaught errors to the terminal - * context.on('pageerror', pageerror => { - * console.log(`Uncaught exception: "${pageerror.error()}"`); - * }); - * - * // Navigate to a page with an exception. - * await page.goto('data:text/html,'); - * ``` - * - */ -export interface PageError { - /** - * Unhandled error that was thrown. - */ - error(): Error; - - /** - * The page that produced this unhandled exception, if any. - */ - page(): null|Page; -} - /** * This object can be used to launch or connect to Chromium, returning instances of {@link Browser}. */ @@ -19024,6 +18991,34 @@ export interface Video { saveAs(path: string): Promise; } +/** + * {@link WebError} class represents an unhandled exeception thrown in the page. It is dispatched via the + * [browserContext.on('weberror')](https://playwright.dev/docs/api/class-browsercontext#browser-context-event-web-error) + * event. + * + * ```js + * // Log all uncaught errors to the terminal + * context.on('weberror', webError => { + * console.log(`Uncaught exception: "${webError.error()}"`); + * }); + * + * // Navigate to a page with an exception. + * await page.goto('data:text/html,'); + * ``` + * + */ +export interface WebError { + /** + * Unhandled error that was thrown. + */ + error(): Error; + + /** + * The page that produced this unhandled exception, if any. + */ + page(): null|Page; +} + /** * The {@link WebSocket} class represents websocket connections in the page. */ diff --git a/tests/library/browsercontext-events.spec.ts b/tests/library/browsercontext-events.spec.ts index 966d1d0852..77d1af1a2e 100644 --- a/tests/library/browsercontext-events.spec.ts +++ b/tests/library/browsercontext-events.spec.ts @@ -161,11 +161,11 @@ test('dialog event should work with inline script tag', async ({ page, server }) await expect.poll(() => popup.evaluate('window.result')).toBe('hello'); }); -test('pageError event should work', async ({ page }) => { - const [pageerror] = await Promise.all([ - page.context().waitForEvent('pageerror'), +test('weberror event should work', async ({ page }) => { + const [webError] = await Promise.all([ + page.context().waitForEvent('weberror'), page.setContent(''), ]); - expect(pageerror.page()).toBe(page); - expect(pageerror.error().stack).toContain('boom'); + expect(webError.page()).toBe(page); + expect(webError.error().stack).toContain('boom'); });