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
Revert "chore: refactor httpServer ws transport" (#40203)
Simon Knott8011759a719f
diff --git a/packages/playwright-core/src/server/trace/viewer/traceViewer.ts b/packages/playwright-core/src/server/trace/viewer/traceViewer.tsindex e9fbf9f51e..fa324e1b1e 100644--- a/packages/playwright-core/src/server/trace/viewer/traceViewer.ts+++ b/packages/playwright-core/src/server/trace/viewer/traceViewer.ts@@ -231,25 +231,23 @@ class StdinServer implements Transport {onconnect() {}- onmessage(message: string) {- const { id, method } = JSON.parse(message);+ async dispatch(method: string, params: any) {if (method === 'initialize') {if (this._traceUrl)this._loadTrace(this._traceUrl);}- this.sendMessage?.(JSON.stringify({ id }));}onclose() {}- sendMessage?: (message: string) => void;+ sendEvent?: (method: string, params: any) => void;close?: () => void;private _loadTrace(traceUrl: string) {this._traceUrl = traceUrl;clearTimeout(this._pollTimer);- this.sendMessage?.(JSON.stringify({ method: 'loadTraceRequested', params: { traceUrl } }));+ this.sendEvent?.('loadTraceRequested', { traceUrl });}private _pollLoadTrace(url: string) {diff --git a/packages/playwright-core/src/tools/dashboard/dashboardController.ts b/packages/playwright-core/src/tools/dashboard/dashboardController.tsindex 3f61d74e50..9fb79198cc 100644--- a/packages/playwright-core/src/tools/dashboard/dashboardController.ts+++ b/packages/playwright-core/src/tools/dashboard/dashboardController.ts@@ -25,7 +25,7 @@ import type { BrowserDescriptor } from '../../serverRegistry';export class DashboardConnection implements Transport, DashboardChannel {readonly version = 1;- sendMessage?: (message: string) => void;+ sendEvent?: (method: string, params: any) => void;close?: () => void;selectedPage: api.Page | null = null;@@ -63,7 +63,7 @@ export class DashboardConnection implements Transport, DashboardChannel {}private _emit<K extends keyof DashboardChannelEvents>(event: K, params: DashboardChannelEvents[K]): void {- this.sendMessage?.(JSON.stringify({ method: event, params }));+ this.sendEvent?.(event, params);const set = this._eventListeners.get(event);if (set) {for (const fn of set)@@ -104,16 +104,10 @@ export class DashboardConnection implements Transport, DashboardChannel {this._browser?.close().catch(() => {});}- async onmessage(message: string) {- const { id, method, params } = JSON.parse(message);+ async dispatch(method: string, params: any): Promise<any> {await this._initPromise;- try {- // eslint-disable-next-line no-restricted-syntax- const result = await (this as any)[method]?.(params);- this.sendMessage?.(JSON.stringify({ id, result }));- } catch (e) {- this.sendMessage?.(JSON.stringify({ id, error: String(e) }));- }+ // eslint-disable-next-line no-restricted-syntax+ return (this as any)[method]?.(params);}async selectTab(params: { pageId: string }) {@@ -317,7 +311,7 @@ async function getBrowserRevision(page: api.Page): Promise<string | null> {}export class CDPConnection implements Transport {- sendMessage?: (message: string) => void;+ sendEvent?: (method: string, params: any) => void;close?: () => void;private _page: api.Page;@@ -333,17 +327,11 @@ export class CDPConnection implements Transport {this._initializePromise = this._initializeRawSession();}- async onmessage(message: string) {- const { id, method, params } = JSON.parse(message);+ async dispatch(method: string, params: any): Promise<any> {await this._initializePromise;if (!this._rawSession)throw new Error('CDP session is not initialized');- try {- const result = await this._rawSession.send(method as Parameters<api.CDPSession['send']>[0], params);- this.sendMessage?.(JSON.stringify({ id, result }));- } catch (e) {- this.sendMessage?.(JSON.stringify({ id, error: String(e) }));- }+ return await this._rawSession.send(method as Parameters<api.CDPSession['send']>[0], params);}onclose() {@@ -358,7 +346,7 @@ export class CDPConnection implements Transport {this._rawSession = session;this._rawSessionListeners = [eventsHelper.addEventListener(session, 'event', ({ method, params }) => {- this.sendMessage?.(JSON.stringify({ method, params }));+ this.sendEvent?.(method, params);}),eventsHelper.addEventListener(session, 'close', () => {this.close?.();diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.tsindex d6e8c0e80e..e5cb70a98e 100644--- a/packages/playwright/src/runner/testServer.ts+++ b/packages/playwright/src/runner/testServer.ts@@ -105,22 +105,14 @@ export class TestServerDispatcher implements TestServerInterface {this._testRunner = new TestRunner(configLocation, configCLIOverrides);this.transport = {onconnect: () => {},- onmessage: async message => {- const { id, method, params } = JSON.parse(message);- try {- const result = await (this as any)[method](params);- this.transport.sendMessage?.(JSON.stringify({ id, result }));- } catch (e) {- this.transport.sendMessage?.(JSON.stringify({ id, error: String(e) }));- }- },+ dispatch: (method, params) => (this as any)[method](params),onclose: () => {if (this._closeOnDisconnect)gracefullyProcessExitDoNotHang(0);},};- this._dispatchEvent = (method, params) => this.transport.sendMessage?.(JSON.stringify({ method, params }));+ this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);this._testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => this._dispatchEvent('testFilesChanged', { testFiles }));this._testRunner.on(TestRunnerEvent.TestPaused, params => this._dispatchEvent('testPaused', { errors: params.errors }));}diff --git a/packages/playwright/src/runner/watchMode.ts b/packages/playwright/src/runner/watchMode.tsindex d405187c8d..706a7ceb39 100644--- a/packages/playwright/src/runner/watchMode.ts+++ b/packages/playwright/src/runner/watchMode.ts@@ -38,9 +38,9 @@ import type { TestServerTransport } from '../isomorphic/testServerConnection';/* eslint-disable no-restricted-properties */class InMemoryTransport extends EventEmitter implements TestServerTransport {- public readonly _send: (message: string) => void;+ public readonly _send: (data: string) => void;- constructor(send: (message: string) => void) {+ constructor(send: (data: any) => void) {super();this._send = send;}@@ -81,9 +81,19 @@ export async function runWatchModeLoop(configLocation: ConfigLocation, initialOplet bufferMode = false;const testServerDispatcher = new TestServerDispatcher(configLocation, {});- const transport = new InMemoryTransport(message => testServerDispatcher.transport.onmessage(message));- testServerDispatcher.transport.sendMessage = message => {- transport.emit('message', message);+ const transport = new InMemoryTransport(+ async data => {+ const { id, method, params } = JSON.parse(data);+ try {+ const result = await testServerDispatcher.transport.dispatch(method, params);+ transport.emit('message', JSON.stringify({ id, result }));+ } catch (e) {+ transport.emit('message', JSON.stringify({ id, error: String(e) }));+ }+ }+ );+ testServerDispatcher.transport.sendEvent = (method, params) => {+ transport.emit('message', JSON.stringify({ method, params }));};const testServerConnection = new TestServerConnection(transport);transport.emit('open');diff --git a/packages/utils/httpServer.ts b/packages/utils/httpServer.tsindex 6f11e83aab..5229f89985 100644--- a/packages/utils/httpServer.ts+++ b/packages/utils/httpServer.ts@@ -28,10 +28,10 @@ import type http from 'http';export type ServerRouteHandler = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;export type Transport = {- sendMessage?: (message: string) => void;+ sendEvent?: (method: string, params: any) => void;close?: () => void;onconnect: () => void;- onmessage: (message: string) => void;+ dispatch: (method: string, params: any) => Promise<any>;onclose: () => void;};@@ -71,10 +71,18 @@ export class HttpServer {wss.on('connection', (ws, request) => {const url = new URL(request.url ?? '/', 'http://localhost');const transport = transportFactory(url);- transport.sendMessage = message => ws.send(message);+ transport.sendEvent = (method, params) => ws.send(JSON.stringify({ method, params }));transport.close = () => ws.close();transport.onconnect();- ws.on('message', message => transport.onmessage(String(message)));+ ws.on('message', async message => {+ const { id, method, params } = JSON.parse(String(message));+ try {+ const result = await transport.dispatch(method, params);+ ws.send(JSON.stringify({ id, result }));+ } catch (e) {+ ws.send(JSON.stringify({ id, error: String(e) }));+ }+ });ws.on('close', () => transport.onclose());ws.on('error', () => transport.onclose());});