Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: migrate to the testserver.initialize (#30226)

Pavel Feldman8ee286b36698
diff --git a/packages/playwright-core/src/server/trace/viewer/traceViewer.ts b/packages/playwright-core/src/server/trace/viewer/traceViewer.ts
index 3f1511cef6..3de6bec1b8 100644
--- a/packages/playwright-core/src/server/trace/viewer/traceViewer.ts
+++ b/packages/playwright-core/src/server/trace/viewer/traceViewer.ts
@@ -216,7 +216,7 @@ class StdinServer implements Transport {
}
async dispatch(method: string, params: any) {
- if (method === 'ready') {
+ if (method === 'initialize') {
if (this._traceUrl)
this._loadTrace(this._traceUrl);
}
diff --git a/packages/playwright/src/isomorphic/testServerConnection.ts b/packages/playwright/src/isomorphic/testServerConnection.ts
index af181abda6..36b4683d95 100644
--- a/packages/playwright/src/isomorphic/testServerConnection.ts
+++ b/packages/playwright/src/isomorphic/testServerConnection.ts
@@ -64,10 +64,7 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
});
const pingInterval = setInterval(() => this._sendMessage('ping').catch(() => {}), 30000);
this._connectedPromise = new Promise<void>((f, r) => {
- this._ws.addEventListener('open', () => {
- f();
- this._ws.send(JSON.stringify({ id: -1, method: 'ready' }));
- });
+ this._ws.addEventListener('open', () => f());
this._ws.addEventListener('error', r);
});
this._ws.addEventListener('close', () => {
@@ -76,10 +73,6 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
});
}
- connect() {
- return this._connectedPromise;
- }
-
private async _sendMessage(method: string, params?: any): Promise<any> {
const logForTest = (globalThis as any).__logForTest;
logForTest?.({ method, params });
@@ -110,8 +103,8 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._onLoadTraceRequestedEmitter.fire(params);
}
- async setSerializer(params: { serializer: string; }): Promise<void> {
- await this._sendMessage('setSerializer', params);
+ async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
+ await this._sendMessage('initialize', params);
}
async ping(params: Parameters<TestServerInterface['ping']>[0]): ReturnType<TestServerInterface['ping']> {
@@ -130,10 +123,6 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._sendMessageNoReply('watch', params);
}
- async watchTestDir(params: Parameters<TestServerInterface['watchTestDir']>[0]): ReturnType<TestServerInterface['watchTestDir']> {
- await this._sendMessage('watchTestDir', params);
- }
-
async open(params: Parameters<TestServerInterface['open']>[0]): ReturnType<TestServerInterface['open']> {
await this._sendMessage('open', params);
}
@@ -190,11 +179,14 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._sendMessageNoReply('stopTests', params);
}
- async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
- await this._sendMessage('setInterceptStdio', params);
- }
-
async closeGracefully(params: Parameters<TestServerInterface['closeGracefully']>[0]): ReturnType<TestServerInterface['closeGracefully']> {
await this._sendMessage('closeGracefully', params);
}
+
+ close() {
+ try {
+ this._ws.close();
+ } catch {
+ }
+ }
}
diff --git a/packages/playwright/src/isomorphic/testServerInterface.ts b/packages/playwright/src/isomorphic/testServerInterface.ts
index 8acfcb4543..34571513b4 100644
--- a/packages/playwright/src/isomorphic/testServerInterface.ts
+++ b/packages/playwright/src/isomorphic/testServerInterface.ts
@@ -21,7 +21,12 @@ import type { JsonEvent } from './teleReceiver';
export type ReportEntry = JsonEvent;
export interface TestServerInterface {
- setSerializer(params: { serializer: string }): Promise<void>;
+ initialize(params: {
+ serializer?: string,
+ closeOnDisconnect?: boolean,
+ interceptStdio?: boolean,
+ watchTestDirs?: boolean,
+ }): Promise<void>;
ping(params: {}): Promise<void>;
@@ -29,8 +34,6 @@ export interface TestServerInterface {
fileNames: string[];
}): Promise<void>;
- watchTestDir(params: {}): Promise<void>;
-
open(params: { location: reporterTypes.Location }): Promise<void>;
resizeTerminal(params: { cols: number, rows: number }): Promise<void>;
@@ -90,8 +93,6 @@ export interface TestServerInterface {
stopTests(params: {}): Promise<void>;
- setInterceptStdio(params: { intercept: boolean }): Promise<void>;
-
closeGracefully(params: {}): Promise<void>;
}
diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.ts
index dc9c2ef49c..7bfec4a583 100644
--- a/packages/playwright/src/runner/testServer.ts
+++ b/packages/playwright/src/runner/testServer.ts
@@ -56,7 +56,7 @@ class TestServer {
}
async stop() {
- await this._dispatcher?.setInterceptStdio({ intercept: false });
+ await this._dispatcher?._setInterceptStdio(false);
await this._dispatcher?.runGlobalTeardown();
}
}
@@ -72,13 +72,17 @@ class TestServerDispatcher implements TestServerInterface {
readonly _dispatchEvent: TestServerInterfaceEventEmitters['dispatchEvent'];
private _plugins: TestRunnerPluginRegistration[] | undefined;
private _serializer = require.resolve('./uiModeReporter');
- private _watchTestDir = false;
+ private _watchTestDirs = false;
+ private _closeOnDisconnect = false;
constructor(configFile: string | undefined) {
this._configFile = configFile;
this.transport = {
dispatch: (method, params) => (this as any)[method](params),
- onclose: () => {},
+ onclose: () => {
+ if (this._closeOnDisconnect)
+ gracefullyProcessExitDoNotHang(0);
+ },
};
this._globalWatcher = new Watcher('deep', () => this._dispatchEvent('listChanged', {}));
this._testWatcher = new Watcher('flat', events => {
@@ -89,10 +93,6 @@ class TestServerDispatcher implements TestServerInterface {
this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);
}
- async setSerializer(params: { serializer: string; }): Promise<void> {
- this._serializer = params.serializer;
- }
-
private async _wireReporter(messageSink: (message: any) => void) {
return await createReporterForTestServer(this._serializer, messageSink);
}
@@ -104,7 +104,16 @@ class TestServerDispatcher implements TestServerInterface {
return { reporter, report };
}
- async ready() {}
+ async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
+ if (params.serializer)
+ this._serializer = params.serializer;
+ if (params.closeOnDisconnect)
+ this._closeOnDisconnect = true;
+ if (params.interceptStdio)
+ await this._setInterceptStdio(true);
+ if (params.watchTestDirs)
+ this._watchTestDirs = true;
+ }
async ping() {}
@@ -226,7 +235,7 @@ class TestServerDispatcher implements TestServerInterface {
projectOutputs.add(result.outDir);
}
- if (this._watchTestDir)
+ if (this._watchTestDirs)
this._globalWatcher.update([...projectDirs], [...projectOutputs], false);
return { report, status };
}
@@ -295,10 +304,6 @@ class TestServerDispatcher implements TestServerInterface {
return { status: await run };
}
- async watchTestDir() {
- this._watchTestDir = true;
- }
-
async watch(params: { fileNames: string[]; }) {
const files = new Set<string>();
for (const fileName of params.fileNames) {
@@ -321,10 +326,10 @@ class TestServerDispatcher implements TestServerInterface {
await this._testRun?.run;
}
- async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
+ async _setInterceptStdio(intercept: boolean) {
if (process.env.PWTEST_DEBUG)
return;
- if (params.intercept) {
+ if (intercept) {
process.stdout.write = (chunk: string | Buffer) => {
this._dispatchEvent('stdio', chunkToPayload('stdout', chunk));
return true;
diff --git a/packages/trace-viewer/src/ui/uiModeView.tsx b/packages/trace-viewer/src/ui/uiModeView.tsx
index 3ff40639f0..03abb468ab 100644
--- a/packages/trace-viewer/src/ui/uiModeView.tsx
+++ b/packages/trace-viewer/src/ui/uiModeView.tsx
@@ -172,8 +172,10 @@ export const UIModeView: React.FC<{}> = ({
setIsLoading(true);
setWatchedTreeIds({ value: new Set() });
(async () => {
- await testServerConnection.setInterceptStdio({ intercept: true });
- await testServerConnection.watchTestDir({});
+ await testServerConnection.initialize({
+ interceptStdio: true,
+ watchTestDirs: true
+ });
const { status } = await testServerConnection.runGlobalSetup({});
if (status !== 'passed')
return;
diff --git a/packages/trace-viewer/src/ui/workbenchLoader.tsx b/packages/trace-viewer/src/ui/workbenchLoader.tsx
index 8efa265ae3..1038d8ac99 100644
--- a/packages/trace-viewer/src/ui/workbenchLoader.tsx
+++ b/packages/trace-viewer/src/ui/workbenchLoader.tsx
@@ -93,6 +93,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
setDragOver(false);
setProcessingErrorMessage(null);
});
+ testServerConnection.initialize({}).catch(() => {});
} else if (!newTraceURLs.some(url => url.startsWith('blob:'))) {
// Don't re-use blob file URLs on page load (results in Fetch error)
setTraceURLs(newTraceURLs);