Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: init eventEmitter w/ platform (#34809)
Pavel Feldman024a52821a61
diff --git a/packages/playwright-core/src/client/android.ts b/packages/playwright-core/src/client/android.tsindex c6eb4225b4..91cf3220ac 100644--- a/packages/playwright-core/src/client/android.ts+++ b/packages/playwright-core/src/client/android.ts@@ -393,7 +393,7 @@ export class AndroidWebView extends EventEmitter implements api.AndroidWebView {private _pagePromise: Promise<Page> | undefined;constructor(device: AndroidDevice, data: channels.AndroidWebView) {- super();+ super(device._platform);this._device = device;this._data = data;}diff --git a/packages/playwright-core/src/client/channelOwner.ts b/packages/playwright-core/src/client/channelOwner.tsindex 3597afd730..163ff3ec0c 100644--- a/packages/playwright-core/src/client/channelOwner.ts+++ b/packages/playwright-core/src/client/channelOwner.ts@@ -38,21 +38,20 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channelreadonly _channel: T;readonly _initializer: channels.InitializerTraits<T>;_logger: Logger | undefined;- readonly _platform: Platform;readonly _instrumentation: ClientInstrumentation;private _eventToSubscriptionMapping: Map<string, string> = new Map();private _isInternalType = false;_wasCollected: boolean = false;constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: channels.InitializerTraits<T>) {- super();+ const connection = parent instanceof ChannelOwner ? parent._connection : parent;+ super(connection._platform);this.setMaxListeners(0);- this._connection = parent instanceof ChannelOwner ? parent._connection : parent;+ this._connection = connection;this._type = type;this._guid = guid;this._parent = parent instanceof ChannelOwner ? parent : undefined;this._instrumentation = this._connection._instrumentation;- this._platform = this._connection.platform;this._connection._objects.set(guid, this);if (this._parent) {@@ -60,7 +59,7 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channelthis._logger = this._parent._logger;}- this._channel = this._createChannel(new EventEmitter());+ this._channel = this._createChannel(new EventEmitter(connection._platform));this._initializer = initializer;}diff --git a/packages/playwright-core/src/client/clientBundle.ts b/packages/playwright-core/src/client/clientBundle.tsindex a81e13daa9..28bfcf2af1 100644--- a/packages/playwright-core/src/client/clientBundle.ts+++ b/packages/playwright-core/src/client/clientBundle.ts@@ -16,14 +16,12 @@import { Connection } from './connection';import { setPlatformForSelectors } from './selectors';-import { setPlatformForEventEmitter } from './eventEmitter';import { setIsUnderTestForValidator } from '../protocol/validatorPrimitives';import type { Platform } from './platform';export function createConnectionFactory(platform: Platform): () => Connection {setPlatformForSelectors(platform);- setPlatformForEventEmitter(platform);setIsUnderTestForValidator(() => platform.isUnderTest());return () => new Connection(platform);}diff --git a/packages/playwright-core/src/client/connection.ts b/packages/playwright-core/src/client/connection.tsindex ad5e16f866..b123728a9b 100644--- a/packages/playwright-core/src/client/connection.ts+++ b/packages/playwright-core/src/client/connection.ts@@ -78,15 +78,13 @@ export class Connection extends EventEmitter {toImpl: ((client: ChannelOwner) => any) | undefined;private _tracingCount = 0;readonly _instrumentation: ClientInstrumentation;- readonly platform: Platform;// Used from @playwright/test fixtures -> TODO remove?readonly headers: HeadersArray;constructor(platform: Platform, localUtils?: LocalUtils, instrumentation?: ClientInstrumentation, headers: HeadersArray = []) {- super();+ super(platform);this._instrumentation = instrumentation || createInstrumentation();this._localUtils = localUtils;- this.platform = platform;this._rootObject = new Root(this);this.headers = headers;}@@ -136,9 +134,9 @@ export class Connection extends EventEmitter {const type = object._type;const id = ++this._lastId;const message = { id, guid, method, params };- if (this.platform.isLogEnabled('channel')) {+ if (this._platform.isLogEnabled('channel')) {// Do not include metadata in debug logs to avoid noise.- this.platform.log('channel', 'SEND> ' + JSON.stringify(message));+ this._platform.log('channel', 'SEND> ' + JSON.stringify(message));}const location = frames[0] ? { file: frames[0].file, line: frames[0].line, column: frames[0].column } : undefined;const metadata: channels.Metadata = { apiName, location, internal: !apiName, stepId };@@ -146,7 +144,7 @@ export class Connection extends EventEmitter {this._localUtils?.addStackToTracingNoReply({ callData: { stack: frames, id } }).catch(() => {});// We need to exit zones before calling into the server, otherwise// when we receive events from the server, we would be in an API zone.- this.platform.zones.empty.run(() => this.onmessage({ ...message, metadata }));+ this._platform.zones.empty.run(() => this.onmessage({ ...message, metadata }));return await new Promise((resolve, reject) => this._callbacks.set(id, { resolve, reject, apiName, type, method }));}@@ -156,15 +154,15 @@ export class Connection extends EventEmitter {const { id, guid, method, params, result, error, log } = message as any;if (id) {- if (this.platform.isLogEnabled('channel'))- this.platform.log('channel', '<RECV ' + JSON.stringify(message));+ if (this._platform.isLogEnabled('channel'))+ this._platform.log('channel', '<RECV ' + JSON.stringify(message));const callback = this._callbacks.get(id);if (!callback)throw new Error(`Cannot find command to respond: ${id}`);this._callbacks.delete(id);if (error && !result) {const parsedError = parseError(error);- rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(this.platform, log));+ rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(this._platform, log));callback.reject(parsedError);} else {const validator = findValidator(callback.type, callback.method, 'Result');@@ -173,8 +171,8 @@ export class Connection extends EventEmitter {return;}- if (this.platform.isLogEnabled('channel'))- this.platform.log('channel', '<EVENT ' + JSON.stringify(message));+ if (this._platform.isLogEnabled('channel'))+ this._platform.log('channel', '<EVENT ' + JSON.stringify(message));if (method === '__create__') {this._createRemoteObject(guid, params.type, params.guid, params.initializer);return;diff --git a/packages/playwright-core/src/client/eventEmitter.ts b/packages/playwright-core/src/client/eventEmitter.tsindex 73d048b274..e1c08619ec 100644--- a/packages/playwright-core/src/client/eventEmitter.ts+++ b/packages/playwright-core/src/client/eventEmitter.ts@@ -22,8 +22,6 @@* USE OR OTHER DEALINGS IN THE SOFTWARE.*/-import { emptyPlatform } from './platform';-import type { EventEmitter as EventEmitterType } from 'events';import type { Platform } from './platform';@@ -31,12 +29,6 @@ type EventType = string | symbol;type Listener = (...args: any[]) => any;type EventMap = Record<EventType, Listener | Listener[]>;-let platform = emptyPlatform;--export function setPlatformForEventEmitter(p: Platform) {- platform = p;-}-export class EventEmitter implements EventEmitterType {private _events: EventMap | undefined = undefined;@@ -44,8 +36,10 @@ export class EventEmitter implements EventEmitterType {private _maxListeners: number | undefined = undefined;readonly _pendingHandlers = new Map<EventType, Set<Promise<void>>>();private _rejectionHandler: ((error: Error) => void) | undefined;+ readonly _platform: Platform;- constructor() {+ constructor(platform: Platform) {+ this._platform = platform;if (this._events === undefined || this._events === Object.getPrototypeOf(this)._events) {this._events = Object.create(null);this._eventsCount = 0;@@ -63,7 +57,7 @@ export class EventEmitter implements EventEmitterType {}getMaxListeners(): number {- return this._maxListeners === undefined ? platform.defaultMaxListeners() : this._maxListeners;+ return this._maxListeners === undefined ? this._platform.defaultMaxListeners() : this._maxListeners;}emit(type: EventType, ...args: any[]): boolean {@@ -161,7 +155,7 @@ export class EventEmitter implements EventEmitterType {w.emitter = this;w.type = type;w.count = existing.length;- if (!platform.isUnderTest()) {+ if (!this._platform.isUnderTest()) {// eslint-disable-next-line no-consoleconsole.warn(w);}diff --git a/packages/playwright-core/src/client/frame.ts b/packages/playwright-core/src/client/frame.tsindex 69d7e2b73c..dafd3f8b2a 100644--- a/packages/playwright-core/src/client/frame.ts+++ b/packages/playwright-core/src/client/frame.ts@@ -64,7 +64,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Frconstructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.FrameInitializer) {super(parent, type, guid, initializer);- this._eventEmitter = new EventEmitter();+ this._eventEmitter = new EventEmitter(parent._platform);this._eventEmitter.setMaxListeners(0);this._parentFrame = Frame.fromNullable(initializer.parentFrame);if (this._parentFrame)diff --git a/packages/playwright-core/src/client/webSocket.ts b/packages/playwright-core/src/client/webSocket.tsindex 6fdd56aa4a..b4d8003789 100644--- a/packages/playwright-core/src/client/webSocket.ts+++ b/packages/playwright-core/src/client/webSocket.ts@@ -24,7 +24,7 @@ export async function connectOverWebSocket(parentConnection: Connection, params:const localUtils = parentConnection.localUtils();const transport = localUtils ? new JsonPipeTransport(localUtils) : new WebSocketTransport();const connectHeaders = await transport.connect(params);- const connection = new Connection(parentConnection.platform, localUtils, parentConnection._instrumentation, connectHeaders);+ const connection = new Connection(parentConnection._platform, localUtils, parentConnection._instrumentation, connectHeaders);connection.markAsRemote();connection.on('close', () => transport.close());diff --git a/tests/library/events/add-listeners.spec.ts b/tests/library/events/add-listeners.spec.tsindex 729b06b0c6..7f339ca039 100644--- a/tests/library/events/add-listeners.spec.ts+++ b/tests/library/events/add-listeners.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test.describe('EventEmitter tests', () => {diff --git a/tests/library/events/check-listener-leaks.spec.ts b/tests/library/events/check-listener-leaks.spec.tsindex 916c5678f2..052f95c87b 100644--- a/tests/library/events/check-listener-leaks.spec.ts+++ b/tests/library/events/check-listener-leaks.spec.ts@@ -21,7 +21,7 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import events from 'events';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { setUnderTest } from '../../../packages/playwright-core/lib/server/utils/debug';import { test, expect } from '@playwright/test';import * as common from './utils';diff --git a/tests/library/events/events-list.spec.ts b/tests/library/events/events-list.spec.tsindex 97d3c8edc3..27f3badb70 100644--- a/tests/library/events/events-list.spec.ts+++ b/tests/library/events/events-list.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test.describe('EventEmitter', () => {diff --git a/tests/library/events/listener-count.spec.ts b/tests/library/events/listener-count.spec.tsindex d8aaf867a3..4161fea2f1 100644--- a/tests/library/events/listener-count.spec.ts+++ b/tests/library/events/listener-count.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('Listener count test', () => {diff --git a/tests/library/events/listeners-side-effects.spec.ts b/tests/library/events/listeners-side-effects.spec.tsindex 176d372dfe..bdf1a0f997 100644--- a/tests/library/events/listeners-side-effects.spec.ts+++ b/tests/library/events/listeners-side-effects.spec.ts@@ -21,7 +21,7 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import { test, expect } from '@playwright/test';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';test('listeners empty check', () => {const e = new EventEmitter();diff --git a/tests/library/events/listeners.spec.ts b/tests/library/events/listeners.spec.tsindex 86803af178..dbc7566fa1 100644--- a/tests/library/events/listeners.spec.ts+++ b/tests/library/events/listeners.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';const listener = () => {};diff --git a/tests/library/events/max-listeners.spec.ts b/tests/library/events/max-listeners.spec.tsindex 21e3582729..424120978f 100644--- a/tests/library/events/max-listeners.spec.ts+++ b/tests/library/events/max-listeners.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH the SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('emit maxListeners on e', () => {diff --git a/tests/library/events/method-names.spec.ts b/tests/library/events/method-names.spec.tsindex 69a0a0ece2..72b512baf8 100644--- a/tests/library/events/method-names.spec.ts+++ b/tests/library/events/method-names.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('EventEmitter prototype test', () => {diff --git a/tests/library/events/modify-in-emit.spec.ts b/tests/library/events/modify-in-emit.spec.tsindex 25937977f4..a364bdbde1 100644--- a/tests/library/events/modify-in-emit.spec.ts+++ b/tests/library/events/modify-in-emit.spec.ts@@ -21,7 +21,7 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import { test, expect } from '@playwright/test';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';let callbacks_called = [];const e = new EventEmitter();diff --git a/tests/library/events/num-args.spec.ts b/tests/library/events/num-args.spec.tsindex cb0b569053..4125cc6ecf 100644--- a/tests/library/events/num-args.spec.ts+++ b/tests/library/events/num-args.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('should work', () => {diff --git a/tests/library/events/once.spec.ts b/tests/library/events/once.spec.tsindex 124c98729b..0e7f5aebfc 100644--- a/tests/library/events/once.spec.ts+++ b/tests/library/events/once.spec.ts@@ -21,7 +21,7 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import { test, expect } from '@playwright/test';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import * as common from './utils';test('should work', () => {diff --git a/tests/library/events/prepend.spec.ts b/tests/library/events/prepend.spec.tsindex 550c01eb3c..2e2de78999 100644--- a/tests/library/events/prepend.spec.ts+++ b/tests/library/events/prepend.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('EventEmitter functionality', () => {diff --git a/tests/library/events/remove-all-listeners-wait.spec.ts b/tests/library/events/remove-all-listeners-wait.spec.tsindex e5b4bf2dc5..dbc141d6d8 100644--- a/tests/library/events/remove-all-listeners-wait.spec.ts+++ b/tests/library/events/remove-all-listeners-wait.spec.ts@@ -16,7 +16,7 @@*/import { ManualPromise } from '../../../packages/playwright-core/lib/utils/isomorphic/manualPromise';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('should not throw with ignoreErrors', async () => {diff --git a/tests/library/events/remove-all-listeners.spec.ts b/tests/library/events/remove-all-listeners.spec.tsindex 275c0700ff..b00f747628 100644--- a/tests/library/events/remove-all-listeners.spec.ts+++ b/tests/library/events/remove-all-listeners.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';import * as common from './utils';diff --git a/tests/library/events/remove-listeners.spec.ts b/tests/library/events/remove-listeners.spec.tsindex 1ea32bd598..012128c519 100644--- a/tests/library/events/remove-listeners.spec.ts+++ b/tests/library/events/remove-listeners.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';import * as common from './utils';diff --git a/tests/library/events/set-max-listeners-side-effects.spec.ts b/tests/library/events/set-max-listeners-side-effects.spec.tsindex c966e9cdf2..17e3ce0ce3 100644--- a/tests/library/events/set-max-listeners-side-effects.spec.ts+++ b/tests/library/events/set-max-listeners-side-effects.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('set max listeners test', () => {diff --git a/tests/library/events/special-event-names.spec.ts b/tests/library/events/special-event-names.spec.tsindex 5d4b6e8e1d..3199ee2811 100644--- a/tests/library/events/special-event-names.spec.ts+++ b/tests/library/events/special-event-names.spec.ts@@ -21,7 +21,7 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import { test, expect } from '@playwright/test';-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';test('should support special event names', () => {diff --git a/tests/library/events/subclass.spec.ts b/tests/library/events/subclass.spec.tsindex 3852657b8d..68d6e073f4 100644--- a/tests/library/events/subclass.spec.ts+++ b/tests/library/events/subclass.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';class MyEE extends EventEmitter {diff --git a/tests/library/events/symbols.spec.ts b/tests/library/events/symbols.spec.tsindex 1c143def37..c78d61476d 100644--- a/tests/library/events/symbols.spec.ts+++ b/tests/library/events/symbols.spec.ts@@ -20,7 +20,7 @@// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.-import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { EventEmitter } from './utils';import { test, expect } from '@playwright/test';test('should support symbols', () => {diff --git a/tests/library/events/utils.ts b/tests/library/events/utils.tsindex 8c4a3b463f..7ba4816376 100644--- a/tests/library/events/utils.ts+++ b/tests/library/events/utils.ts@@ -20,6 +20,8 @@// USE OR OTHER DEALINGS IN THE SOFTWARE.import { expect } from '@playwright/test';+import { EventEmitter as OriginalEventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';+import { nodePlatform } from '../../../packages/playwright-core/lib/server/utils/nodePlatform';export const mustNotCall = (msg?: string) => {return function mustNotCall() {@@ -44,3 +46,9 @@ export const mustCall = (fn?: Function, exact?: number) => {--count;};};++export class EventEmitter extends OriginalEventEmitter {+ constructor() {+ super(nodePlatform);+ }+}