revert subscription logic

This commit is contained in:
Simon Knott 2025-01-27 12:24:51 +01:00
parent c5aad394be
commit 27f8be2fe0
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
4 changed files with 22 additions and 25 deletions

View file

@ -69,8 +69,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
_closeWasCalled = false; _closeWasCalled = false;
private _closeReason: string | undefined; private _closeReason: string | undefined;
private _harRouters: HarRouter[] = []; private _harRouters: HarRouter[] = [];
private _mockingProxies = new Set<MockingProxy>();
_mockingProxies = new Set<MockingProxy>();
static from(context: channels.BrowserContextChannel): BrowserContext { static from(context: channels.BrowserContextChannel): BrowserContext {
return (context as any)._object; return (context as any)._object;
@ -229,6 +228,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await route._innerContinue(true /* isFallback */).catch(() => {}); await route._innerContinue(true /* isFallback */).catch(() => {});
} }
private _onRouteListener = (route: network.Route) => this._onRoute(route);
async _onWebSocketRoute(webSocketRoute: network.WebSocketRoute) { async _onWebSocketRoute(webSocketRoute: network.WebSocketRoute) {
const routeHandler = this._webSocketRoutes.find(route => route.matches(webSocketRoute.url())); const routeHandler = this._webSocketRoutes.find(route => route.matches(webSocketRoute.url()));
if (routeHandler) if (routeHandler)
@ -244,6 +245,11 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await bindingCall.call(func); await bindingCall.call(func);
} }
_subscribeToMockingProxy(mockingProxy: MockingProxy) {
this._mockingProxies.add(mockingProxy);
mockingProxy.on(Events.MockingProxy.Route, this._onRouteListener);
}
setDefaultNavigationTimeout(timeout: number | undefined) { setDefaultNavigationTimeout(timeout: number | undefined) {
this._timeoutSettings.setDefaultNavigationTimeout(timeout); this._timeoutSettings.setDefaultNavigationTimeout(timeout);
this._wrapApiCall(async () => { this._wrapApiCall(async () => {
@ -465,7 +471,9 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
this._disposeHarRouters(); this._disposeHarRouters();
this.tracing._resetStackCounter(); this.tracing._resetStackCounter();
this.emit(Events.BrowserContext.Close, this); this.emit(Events.BrowserContext.Close, this);
this._mockingProxies.forEach(p => p.uninstall(this)); this._mockingProxies.forEach(p => {
p.off(Events.MockingProxy.Route, this._onRouteListener);
});
} }
async [Symbol.asyncDispose]() { async [Symbol.asyncDispose]() {

View file

@ -94,4 +94,8 @@ export const Events = {
Console: 'console', Console: 'console',
Window: 'window', Window: 'window',
}, },
MockingProxy: {
Route: 'rooute',
},
}; };

View file

@ -17,40 +17,23 @@ import * as network from './network';
import type * as channels from '@protocol/channels'; import type * as channels from '@protocol/channels';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
import { APIRequestContext } from './fetch'; import { APIRequestContext } from './fetch';
import type { BrowserContext } from './browserContext';
export class MockingProxy extends ChannelOwner<channels.MockingProxyChannel> { export class MockingProxy extends ChannelOwner<channels.MockingProxyChannel> {
private _port: number; private _port: number;
private _requestContext: APIRequestContext;
private _contexts = new Set<BrowserContext>();
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.MockingProxyInitializer) { constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.MockingProxyInitializer) {
super(parent, type, guid, initializer); super(parent, type, guid, initializer);
this._port = initializer.port; this._port = initializer.port;
this._requestContext = APIRequestContext.from(initializer.requestContext); const requestContext = APIRequestContext.from(initializer.requestContext);
this._channel.on('route', (params: channels.MockingProxyRouteEvent) => { this._channel.on('route', (params: channels.MockingProxyRouteEvent) => {
const route = network.Route.from(params.route); const route = network.Route.from(params.route);
route._context = this._requestContext; route._context = requestContext;
for (const context of this._contexts) { this.emit('route', route);
context._onRoute(route);
for (const page of context.pages())
page._onRoute(route);
}
}); });
} }
install(context: BrowserContext): void {
context._mockingProxies.add(this);
this._contexts.add(context);
}
uninstall(context: BrowserContext): void {
context._mockingProxies.delete(this);
this._contexts.delete(context);
}
async setInterceptionPatterns(params: channels.MockingProxySetInterceptionPatternsParams) { async setInterceptionPatterns(params: channels.MockingProxySetInterceptionPatternsParams) {
await this._channel.setInterceptionPatterns(params); await this._channel.setInterceptionPatterns(params);
} }

View file

@ -25,6 +25,7 @@ import { rootTestType } from './common/testType';
import type { ContextReuseMode } from './common/config'; import type { ContextReuseMode } from './common/config';
import type { ApiCallData, ClientInstrumentation, ClientInstrumentationListener } from '../../playwright-core/src/client/clientInstrumentation'; import type { ApiCallData, ClientInstrumentation, ClientInstrumentationListener } from '../../playwright-core/src/client/clientInstrumentation';
import type { MockingProxy } from '../../playwright-core/src/client/mockingProxy'; import type { MockingProxy } from '../../playwright-core/src/client/mockingProxy';
import type { BrowserContext as BrowserContextImpl } from '../../playwright-core/src/client/browserContext';
import { currentTestInfo } from './common/globals'; import { currentTestInfo } from './common/globals';
import type { LocalUtils } from 'playwright-core/lib/client/localUtils'; import type { LocalUtils } from 'playwright-core/lib/client/localUtils';
export { expect } from './matchers/expect'; export { expect } from './matchers/expect';
@ -369,8 +370,9 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
size: typeof video === 'string' ? undefined : video.size, size: typeof video === 'string' ? undefined : video.size,
} }
} : {}; } : {};
const context = await browser.newContext({ ...videoOptions, ...options }); const context = await browser.newContext({ ...videoOptions, ...options }) as BrowserContextImpl;
_mockingProxy?.install(context as any); if (_mockingProxy)
context._subscribeToMockingProxy(_mockingProxy);
const contextData: { pagesWithVideo: Page[] } = { pagesWithVideo: [] }; const contextData: { pagesWithVideo: Page[] } = { pagesWithVideo: [] };
contexts.set(context, contextData); contexts.set(context, contextData);
if (captureVideo) if (captureVideo)