Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -1310,10 +1310,13 @@ Optional setting to control resource content management. If `attach` is specifie

## async method: BrowserContext.routeWebSocket
* since: v1.48
- returns: <[Disposable]>

This method allows to modify websocket connections that are made by any page in the browser context.

Note that only `WebSocket`s created after this method was called will be routed. It is recommended to call this method before creating any pages.
:::note
Only affects `WebSocket` created after this method was called will be routed. It is recommended to call this method before creating any pages.
:::

**Usage**

Expand Down Expand Up @@ -1618,6 +1621,15 @@ Removes all routes created with [`method: BrowserContext.route`] and [`method: B
### option: BrowserContext.unrouteAll.behavior = %%-unroute-all-options-behavior-%%
* since: v1.41

## async method: BrowserContext.unrouteAllWebSockets
* since: v1.61

Removes all routes created with [`method: BrowserContext.routeWebSocket`].

:::note
Only affects `WebSocket` created after this call. Any [WebSocketRoute] already passed to a handler will keep working until the underlying `WebSocket` is closed.
:::

## async method: BrowserContext.unroute
* since: v1.8

Expand Down Expand Up @@ -1654,6 +1666,46 @@ Optional handler function used to register a routing with [`method: BrowserConte

Optional handler function used to register a routing with [`method: BrowserContext.route`].

## async method: BrowserContext.unrouteWebSocket
* since: v1.61

Removes a route created with [`method: BrowserContext.routeWebSocket`]. When [`param: handler`] is not specified,
removes all routes for the [`param: url`].

:::note
Only affects `WebSocket` created after this call. Any [WebSocketRoute] already passed to a handler will keep working until the underlying `WebSocket` is closed.
:::

### param: BrowserContext.unrouteWebSocket.url
* since: v1.61
* langs: js
- `url` <[string]|[RegExp]|[URLPattern]|[function]\([URL]\):[boolean]>

A glob pattern, regex pattern, URL pattern, or predicate receiving [URL] used to register a routing with
[`method: BrowserContext.routeWebSocket`].

### param: BrowserContext.unrouteWebSocket.url
* since: v1.61
* langs: python, csharp, java
- `url` <[string]|[RegExp]|[function]\([URL]\):[boolean]>

A glob pattern, regex pattern, or predicate receiving [URL] used to register a routing with
[`method: BrowserContext.routeWebSocket`].

### param: BrowserContext.unrouteWebSocket.handler
* since: v1.61
* langs: js, python
- `handler` ?<[function]\([WebSocketRoute]\): [Promise<any>|any]>

Optional handler function used to register a routing with [`method: BrowserContext.routeWebSocket`].

### param: BrowserContext.unrouteWebSocket.handler
* since: v1.61
* langs: csharp, java
- `handler` ?<[function]\([WebSocketRoute]\)>

Optional handler function used to register a routing with [`method: BrowserContext.routeWebSocket`].

## async method: BrowserContext.waitForCondition
* since: v1.32
* langs: java
Expand Down
54 changes: 53 additions & 1 deletion docs/src/api/class-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -3811,10 +3811,13 @@ Optional setting to control resource content management. If `attach` is specifie

## async method: Page.routeWebSocket
* since: v1.48
- returns: <[Disposable]>

This method allows to modify websocket connections that are made by the page.

Note that only `WebSocket`s created after this method was called will be routed. It is recommended to call this method before navigating the page.
:::note
Only affects `WebSocket` created after this method was called will be routed. It is recommended to call this method before navigating the page.
:::

**Usage**

Expand Down Expand Up @@ -4446,6 +4449,15 @@ Removes all routes created with [`method: Page.route`] and [`method: Page.routeF
### option: Page.unrouteAll.behavior = %%-unroute-all-options-behavior-%%
* since: v1.41

## async method: Page.unrouteAllWebSockets
* since: v1.61

Removes all routes created with [`method: Page.routeWebSocket`].

:::note
Only affects `WebSocket` created after this call. Any [WebSocketRoute] already passed to a handler will keep working until the underlying `WebSocket` is closed.
:::

## async method: Page.unroute
* since: v1.8

Expand Down Expand Up @@ -4480,6 +4492,46 @@ Optional handler function to route the request.

Optional handler function to route the request.

## async method: Page.unrouteWebSocket
* since: v1.61

Removes a route created with [`method: Page.routeWebSocket`]. When [`param: handler`] is not specified, removes all
routes for the [`param: url`].

:::note
Only affects `WebSocket` created after this call. Any [WebSocketRoute] already passed to a handler will keep working until the underlying `WebSocket` is closed.
:::

### param: Page.unrouteWebSocket.url
* since: v1.61
* langs: js
- `url` <[string]|[RegExp]|[URLPattern]|[function]\([URL]\):[boolean]>

A glob pattern, regex pattern, URL pattern, or predicate receiving [URL] used to register a routing with
[`method: Page.routeWebSocket`].

### param: Page.unrouteWebSocket.url
* since: v1.61
* langs: python, csharp, java
- `url` <[string]|[RegExp]|[function]\([URL]\):[boolean]>

A glob pattern, regex pattern, or predicate receiving [URL] used to register a routing with
[`method: Page.routeWebSocket`].

### param: Page.unrouteWebSocket.handler
* since: v1.61
* langs: js, python
- `handler` ?<[function]\([WebSocketRoute]\): [Promise<any>|any]>

Optional handler function used to register a routing with [`method: Page.routeWebSocket`].

### param: Page.unrouteWebSocket.handler
* since: v1.61
* langs: csharp, java
- `handler` ?<[function]\([WebSocketRoute]\)>

Optional handler function used to register a routing with [`method: Page.routeWebSocket`].

## method: Page.url
* since: v1.8
- returns: <[string]>
Expand Down
70 changes: 64 additions & 6 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4129,8 +4129,8 @@ export interface Page {
/**
* This method allows to modify websocket connections that are made by the page.
*
* Note that only `WebSocket`s created after this method was called will be routed. It is recommended to call this
* method before navigating the page.
* **NOTE** Only affects `WebSocket` created after this method was called will be routed. It is recommended to call
* this method before navigating the page.
*
* **Usage**
*
Expand All @@ -4150,7 +4150,7 @@ export interface Page {
* [`baseURL`](https://playwright.dev/docs/api/class-browser#browser-new-context-option-base-url) context option.
* @param handler Handler function to route the WebSocket.
*/
routeWebSocket(url: string|RegExp|URLPattern|((url: URL) => boolean), handler: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<void>;
routeWebSocket(url: string|RegExp|URLPattern|((url: URL) => boolean), handler: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<Disposable>;

/**
* Returns the buffer with the captured screenshot.
Expand Down Expand Up @@ -4764,6 +4764,34 @@ export interface Page {
behavior?: "wait"|"ignoreErrors"|"default";
}): Promise<void>;

/**
* Removes all routes created with
* [page.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-page#page-route-web-socket).
*
* **NOTE** Only affects `WebSocket` created after this call. Any
* [WebSocketRoute](https://playwright.dev/docs/api/class-websocketroute) already passed to a handler will keep
* working until the underlying `WebSocket` is closed.
*
*/
unrouteAllWebSockets(): Promise<void>;

/**
* Removes a route created with
* [page.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-page#page-route-web-socket). When
* [`handler`](https://playwright.dev/docs/api/class-page#page-unroute-web-socket-option-handler) is not specified,
* removes all routes for the [`url`](https://playwright.dev/docs/api/class-page#page-unroute-web-socket-option-url).
*
* **NOTE** Only affects `WebSocket` created after this call. Any
* [WebSocketRoute](https://playwright.dev/docs/api/class-websocketroute) already passed to a handler will keep
* working until the underlying `WebSocket` is closed.
*
* @param url A glob pattern, regex pattern, URL pattern, or predicate receiving [URL] used to register a routing with
* [page.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-page#page-route-web-socket).
* @param handler Optional handler function used to register a routing with
* [page.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-page#page-route-web-socket).
*/
unrouteWebSocket(url: string|RegExp|URLPattern|((url: URL) => boolean), handler?: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<void>;

url(): string;

/**
Expand Down Expand Up @@ -9505,8 +9533,8 @@ export interface BrowserContext {
/**
* This method allows to modify websocket connections that are made by any page in the browser context.
*
* Note that only `WebSocket`s created after this method was called will be routed. It is recommended to call this
* method before creating any pages.
* **NOTE** Only affects `WebSocket` created after this method was called will be routed. It is recommended to call
* this method before creating any pages.
*
* **Usage**
*
Expand All @@ -9528,7 +9556,7 @@ export interface BrowserContext {
* [`baseURL`](https://playwright.dev/docs/api/class-browser#browser-new-context-option-base-url) context option.
* @param handler Handler function to route the WebSocket.
*/
routeWebSocket(url: string|RegExp|((url: URL) => boolean), handler: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<void>;
routeWebSocket(url: string|RegExp|((url: URL) => boolean), handler: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<Disposable>;

/**
* **NOTE** Service workers are only supported on Chromium-based browsers.
Expand Down Expand Up @@ -9784,6 +9812,36 @@ export interface BrowserContext {
behavior?: "wait"|"ignoreErrors"|"default";
}): Promise<void>;

/**
* Removes all routes created with
* [browserContext.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-browsercontext#browser-context-route-web-socket).
*
* **NOTE** Only affects `WebSocket` created after this call. Any
* [WebSocketRoute](https://playwright.dev/docs/api/class-websocketroute) already passed to a handler will keep
* working until the underlying `WebSocket` is closed.
*
*/
unrouteAllWebSockets(): Promise<void>;

/**
* Removes a route created with
* [browserContext.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-browsercontext#browser-context-route-web-socket).
* When
* [`handler`](https://playwright.dev/docs/api/class-browsercontext#browser-context-unroute-web-socket-option-handler)
* is not specified, removes all routes for the
* [`url`](https://playwright.dev/docs/api/class-browsercontext#browser-context-unroute-web-socket-option-url).
*
* **NOTE** Only affects `WebSocket` created after this call. Any
* [WebSocketRoute](https://playwright.dev/docs/api/class-websocketroute) already passed to a handler will keep
* working until the underlying `WebSocket` is closed.
*
* @param url A glob pattern, regex pattern, URL pattern, or predicate receiving [URL] used to register a routing with
* [browserContext.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-browsercontext#browser-context-route-web-socket).
* @param handler Optional handler function used to register a routing with
* [browserContext.routeWebSocket(url, handler)](https://playwright.dev/docs/api/class-browsercontext#browser-context-route-web-socket).
*/
unrouteWebSocket(url: string|RegExp|URLPattern|((url: URL) => boolean), handler?: ((websocketroute: WebSocketRoute) => Promise<any>|any)): Promise<void>;

/**
* This event is not emitted.
*/
Expand Down
13 changes: 12 additions & 1 deletion packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
return new DisposableStub(() => this.unroute(url, handler));
}

async routeWebSocket(url: URLMatch, handler: network.WebSocketRouteHandlerCallback): Promise<void> {
async routeWebSocket(url: URLMatch, handler: network.WebSocketRouteHandlerCallback): Promise<DisposableStub> {
this._webSocketRoutes.unshift(new network.WebSocketRouteHandler(this._options.baseURL, url, handler));
await this._updateWebSocketInterceptionPatterns({ title: 'Route WebSockets' });
return new DisposableStub(() => this.unrouteWebSocket(url, handler));
}

async routeFromHAR(har: string, options: { url?: string | RegExp, notFound?: 'abort' | 'fallback', update?: boolean, updateContent?: 'attach' | 'embed', updateMode?: 'minimal' | 'full' } = {}): Promise<void> {
Expand All @@ -408,6 +409,11 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
this._disposeHarRouters();
}

async unrouteAllWebSockets(): Promise<void> {
this._webSocketRoutes = [];
await this._updateWebSocketInterceptionPatterns({ title: 'Unroute WebSockets' });
}

async unroute(url: URLMatch, handler?: network.RouteHandlerCallback): Promise<void> {
const removed = [];
const remaining = [];
Expand All @@ -420,6 +426,11 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await this._unrouteInternal(removed, remaining, 'default');
}

async unrouteWebSocket(url: URLMatch, handler?: network.WebSocketRouteHandlerCallback): Promise<void> {
this._webSocketRoutes = this._webSocketRoutes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler));
await this._updateWebSocketInterceptionPatterns({ title: 'Unroute WebSockets' });
}

private async _unrouteInternal(removed: network.RouteHandler[], remaining: network.RouteHandler[], behavior?: 'wait'|'ignoreErrors'|'default'): Promise<void> {
this._routes = remaining;
if (behavior && behavior !== 'default') {
Expand Down
13 changes: 12 additions & 1 deletion packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,10 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
await harRouter.addPageRoute(this);
}

async routeWebSocket(url: URLMatch, handler: WebSocketRouteHandlerCallback): Promise<void> {
async routeWebSocket(url: URLMatch, handler: WebSocketRouteHandlerCallback): Promise<DisposableStub> {
this._webSocketRoutes.unshift(new WebSocketRouteHandler(this._browserContext._options.baseURL, url, handler));
await this._updateWebSocketInterceptionPatterns({ title: 'Route WebSockets' });
return new DisposableStub(() => this.unrouteWebSocket(url, handler));
}

private _disposeHarRouters() {
Expand All @@ -568,6 +569,11 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
this._disposeHarRouters();
}

async unrouteAllWebSockets(): Promise<void> {
this._webSocketRoutes = [];
await this._updateWebSocketInterceptionPatterns({ title: 'Unroute WebSockets' });
}

async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise<void> {
const removed = [];
const remaining = [];
Expand All @@ -580,6 +586,11 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
await this._unrouteInternal(removed, remaining, 'default');
}

async unrouteWebSocket(url: URLMatch, handler?: WebSocketRouteHandlerCallback): Promise<void> {
this._webSocketRoutes = this._webSocketRoutes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler));
await this._updateWebSocketInterceptionPatterns({ title: 'Unroute WebSockets' });
}

private async _unrouteInternal(removed: RouteHandler[], remaining: RouteHandler[], behavior?: 'wait'|'ignoreErrors'|'default'): Promise<void> {
this._routes = remaining;
if (behavior && behavior !== 'default') {
Expand Down
Loading
Loading