Skip to content

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

chore: do not mute console for mcp runner (#37233)

Pavel Feldman7d43a4574441
diff --git a/packages/playwright/src/mcp/test/backend.ts b/packages/playwright/src/mcp/test/backend.ts
index 30f9f7b8eb..cb0a1b0733 100644
--- a/packages/playwright/src/mcp/test/backend.ts
+++ b/packages/playwright/src/mcp/test/backend.ts
@@ -48,11 +48,7 @@ export class TestServerBackend implements mcp.ServerBackend {
if (!tool)
throw new Error(`Tool not found: ${name}. Available tools: ${this._tools.map(tool => tool.schema.name).join(', ')}`);
const parsedArguments = tool.schema.inputSchema.parse(args || {});
- const result = await tool.handle(this._context!, parsedArguments);
- const stdio = this._context.takeStdio();
- if (stdio.trim())
- result.content.push({ type: 'text', text: stdio });
- return result;
+ return await tool.handle(this._context!, parsedArguments);
}
serverClosed() {
diff --git a/packages/playwright/src/mcp/test/context.ts b/packages/playwright/src/mcp/test/context.ts
index 05dcd8aa7b..602abc97e1 100644
--- a/packages/playwright/src/mcp/test/context.ts
+++ b/packages/playwright/src/mcp/test/context.ts
@@ -22,7 +22,6 @@ export class Context {
private _testRunner: TestRunner | undefined;
readonly configLocation: ConfigLocation;
readonly options?: { muteConsole?: boolean };
- private _stdio: { chunk: string | Buffer, stdio: 'stdout' | 'stderr' }[] = [];
constructor(configLocation: ConfigLocation, options?: { muteConsole?: boolean }) {
this.configLocation = configLocation;
@@ -33,13 +32,7 @@ export class Context {
if (this._testRunner)
await this._testRunner.stopTests();
const testRunner = new TestRunner(this.configLocation, {});
- await testRunner.initialize({
- sendStdioEvents: true,
- muteConsole: this.options?.muteConsole,
- });
- testRunner.on(TestRunnerEvent.StdioChunk, (chunk, stdio) => {
- this._stdio.push({ chunk, stdio });
- });
+ await testRunner.initialize({});
this._testRunner = testRunner;
testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => {
this._testRunner?.emit(TestRunnerEvent.TestFilesChanged, testFiles);
@@ -48,18 +41,6 @@ export class Context {
return testRunner;
}
- takeStdio(): string {
- const text = this._stdio.map(entry => chunkToPayload(entry.stdio, entry.chunk)).join('\n');
- this._stdio = [];
- return text;
- }
-
async close() {
}
}
-
-function chunkToPayload(type: 'stdout' | 'stderr', chunk: Buffer | string): string {
- if (chunk instanceof Uint8Array)
- return '<binary>';
- return `[${type}] ${chunk}`;
-}
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index b2612e7aa0..8255f67ae3 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -17,9 +17,7 @@
import EventEmitter from 'events';
import fs from 'fs';
import path from 'path';
-import util from 'util';
-import { debug } from 'playwright-core/lib/utilsBundle';
import { registry } from 'playwright-core/lib/server';
import { ManualPromise, gracefullyProcessExitDoNotHang } from 'playwright-core/lib/utils';
@@ -44,12 +42,10 @@ import type { AnyReporter } from '../reporters/reporterV2';
export const TestRunnerEvent = {
TestFilesChanged: 'testFilesChanged',
- StdioChunk: 'stdioChunk',
} as const;
export type TestRunnerEventMap = {
[TestRunnerEvent.TestFilesChanged]: [testFiles: string[]];
- [TestRunnerEvent.StdioChunk]: [chunk: string | Buffer, stdio: 'stdout' | 'stderr'];
};
export type ListTestsParams = {
@@ -79,12 +75,6 @@ export type RunTestsParams = {
type FullResultStatus = reporterTypes.FullResult['status'];
-const originalDebugLog = debug.log;
-// eslint-disable-next-line no-restricted-properties
-const originalStdoutWrite = process.stdout.write;
-// eslint-disable-next-line no-restricted-properties
-const originalStderrWrite = process.stderr.write;
-
export class TestRunner extends EventEmitter<TestRunnerEventMap> {
readonly configLocation: ConfigLocation;
private _configCLIOverrides: ConfigCLIOverrides;
@@ -116,15 +106,9 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
async initialize(params: {
watchTestDirs?: boolean;
populateDependenciesOnList?: boolean;
- sendStdioEvents?: boolean;
- muteConsole?: boolean;
}) {
this._watchTestDirs = !!params.watchTestDirs;
this._populateDependenciesOnList = !!params.populateDependenciesOnList;
- this._setInterceptStdio({
- sendStdioEvents: !!params.sendStdioEvents,
- muteConsole: !!params.muteConsole,
- });
}
resizeTerminal(params: { cols: number, rows: number }) {
@@ -394,7 +378,6 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
}
async stop() {
- this._setInterceptStdio({ sendStdioEvents: false, muteConsole: false });
await this.runGlobalTeardown();
}
@@ -426,42 +409,6 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
await reporter.onExit();
return null;
}
-
- _setInterceptStdio(options: { sendStdioEvents: boolean, muteConsole: boolean }) {
- /* eslint-disable no-restricted-properties */
- if (process.env.PWTEST_DEBUG)
- return;
- if (options.sendStdioEvents || options.muteConsole) {
- if (debug.log === originalDebugLog) {
- // Only if debug.log hasn't already been tampered with, don't intercept any DEBUG=* logging
- debug.log = (...args) => {
- const string = util.format(...args) + '\n';
- return (originalStderrWrite as any).apply(process.stderr, [string]);
- };
- }
- const stdoutWrite = (chunk: string | Buffer) => {
- if (options.sendStdioEvents)
- this.emit(TestRunnerEvent.StdioChunk, chunk, 'stdout');
- if (!options.muteConsole)
- originalStdoutWrite.apply(process.stdout, [chunk]);
- return true;
- };
- const stderrWrite = (chunk: string | Buffer) => {
- if (options.sendStdioEvents)
- this.emit(TestRunnerEvent.StdioChunk, chunk, 'stderr');
- if (!options.muteConsole)
- originalStderrWrite.apply(process.stderr, [chunk]);
- return true;
- };
- process.stdout.write = stdoutWrite;
- process.stderr.write = stderrWrite;
- } else {
- debug.log = originalDebugLog;
- process.stdout.write = originalStdoutWrite;
- process.stderr.write = originalStderrWrite;
- }
- /* eslint-enable no-restricted-properties */
- }
}
function printInternalError(e: Error) {
diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.ts
index 616fd6a53b..26ac3a0e52 100644
--- a/packages/playwright/src/runner/testServer.ts
+++ b/packages/playwright/src/runner/testServer.ts
@@ -14,9 +14,11 @@
* limitations under the License.
*/
+import util from 'util';
+
import { installRootRedirect, openTraceInBrowser, openTraceViewerApp, startTraceViewerServer } from 'playwright-core/lib/server';
import { ManualPromise, gracefullyProcessExitDoNotHang, isUnderTest } from 'playwright-core/lib/utils';
-import { open } from 'playwright-core/lib/utilsBundle';
+import { debug, open } from 'playwright-core/lib/utilsBundle';
import { loadConfig, resolveConfigLocation } from '../common/configLoader';
import ListReporter from '../reporters/list';
@@ -32,6 +34,12 @@ import type { ConfigCLIOverrides } from '../common/ipc';
import type { ReportEntry, TestServerInterface, TestServerInterfaceEventEmitters } from '../isomorphic/testServerInterface';
import type { ReporterV2 } from '../reporters/reporterV2';
+const originalDebugLog = debug.log;
+// eslint-disable-next-line no-restricted-properties
+const originalStdoutWrite = process.stdout.write;
+// eslint-disable-next-line no-restricted-properties
+const originalStderrWrite = process.stderr.write;
+
class TestServer {
private _configLocation: ConfigLocation;
private _configCLIOverrides: ConfigCLIOverrides;
@@ -98,7 +106,6 @@ export class TestServerDispatcher implements TestServerInterface {
this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);
this._testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => this._dispatchEvent('testFilesChanged', { testFiles }));
- this._testRunner.on(TestRunnerEvent.StdioChunk, (chunk, stdio) => this._dispatchEvent('stdio', chunkToPayload(stdio, chunk)));
}
private async _wireReporter(messageSink: (message: any) => void) {
@@ -119,9 +126,8 @@ export class TestServerDispatcher implements TestServerInterface {
this._closeOnDisconnect = !!params.closeOnDisconnect;
await this._testRunner.initialize({
...params,
- sendStdioEvents: !!params.interceptStdio,
- muteConsole: !!params.interceptStdio,
});
+ this._setInterceptStdio(!!params.interceptStdio);
}
async ping() {}
@@ -211,12 +217,43 @@ export class TestServerDispatcher implements TestServerInterface {
}
async stop() {
+ this._setInterceptStdio(false);
await this._testRunner.stop();
}
async closeGracefully() {
await this._testRunner.closeGracefully();
}
+
+ private _setInterceptStdio(interceptStdio: boolean) {
+ /* eslint-disable no-restricted-properties */
+ if (process.env.PWTEST_DEBUG)
+ return;
+ if (interceptStdio) {
+ if (debug.log === originalDebugLog) {
+ // Only if debug.log hasn't already been tampered with, don't intercept any DEBUG=* logging
+ debug.log = (...args) => {
+ const string = util.format(...args) + '\n';
+ return (originalStderrWrite as any).apply(process.stderr, [string]);
+ };
+ }
+ const stdoutWrite = (chunk: string | Buffer) => {
+ this._dispatchEvent('stdio', chunkToPayload('stdout', chunk));
+ return true;
+ };
+ const stderrWrite = (chunk: string | Buffer) => {
+ this._dispatchEvent('stdio', chunkToPayload('stderr', chunk));
+ return true;
+ };
+ process.stdout.write = stdoutWrite;
+ process.stderr.write = stderrWrite;
+ } else {
+ debug.log = originalDebugLog;
+ process.stdout.write = originalStdoutWrite;
+ process.stderr.write = originalStderrWrite;
+ }
+ /* eslint-enable no-restricted-properties */
+ }
}
export async function runUIMode(configFile: string | undefined, configCLIOverrides: ConfigCLIOverrides, options: TraceViewerServerOptions & TraceViewerRedirectOptions): Promise<reporterTypes.FullResult['status']> {