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 bind remote browsers twice (#39627)

Pavel Feldman2abb2a09a158
diff --git a/packages/playwright-core/src/cli/daemon/program.ts b/packages/playwright-core/src/cli/daemon/program.ts
index 5ce7ceefa1..eb814b2d9f 100644
--- a/packages/playwright-core/src/cli/daemon/program.ts
+++ b/packages/playwright-core/src/cli/daemon/program.ts
@@ -53,12 +53,10 @@ export function decorateCLICommand(command: Command, version: string) {
const socketPath = await startCliDaemonServer(sessionName, browserContext, mcpConfig, clientInfo, { persistent, exitOnClose: true });
console.log(`### Success\nDaemon listening on ${socketPath}`);
console.log('<EOF>');
- try {
+
+ if (!(browser as any)._connection.isRemote()) {
await (browser as any)._startServer(sessionName, { workspaceDir: clientInfo.workspaceDir });
browserContext.on('close', () => (browser as any)._stopServer().catch(() => {}));
- } catch (error) {
- if (!error.message.includes('Server is already running'))
- throw error;
}
} catch (error) {
const message = process.env.PWDEBUGIMPL ? (error as Error).stack || (error as Error).message : (error as Error).message;
diff --git a/packages/playwright-core/src/client/browser.ts b/packages/playwright-core/src/client/browser.ts
index 0ab453827a..df4f9aff42 100644
--- a/packages/playwright-core/src/client/browser.ts
+++ b/packages/playwright-core/src/client/browser.ts
@@ -24,7 +24,7 @@ import { mkdirIfNeeded } from './fileUtils';
import type { BrowserType } from './browserType';
import type { Page } from './page';
-import type { BrowserContextOptions, LaunchOptions, Logger } from './types';
+import type { BrowserContextOptions, LaunchOptions, Logger, StartServerOptions } from './types';
import type * as api from '../../types/types';
import type * as channels from '@protocol/channels';
@@ -130,7 +130,7 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
return this._initializer.version;
}
- async _startServer(title: string, options: { wsPath?: string, workspaceDir?: string } = {}): Promise<{ wsEndpoint?: string, pipeName?: string }> {
+ async _startServer(title: string, options: StartServerOptions = {}): Promise<{ wsEndpoint?: string, pipeName?: string }> {
return await this._channel.startServer({ title, ...options });
}
diff --git a/packages/playwright-core/src/client/types.ts b/packages/playwright-core/src/client/types.ts
index 7191942df6..b5a526ab94 100644
--- a/packages/playwright-core/src/client/types.ts
+++ b/packages/playwright-core/src/client/types.ts
@@ -120,6 +120,14 @@ export type LaunchAndroidServerOptions = {
wsPath?: string,
};
+export type StartServerOptions = {
+ host?: string,
+ port?: number,
+ wsPath?: string,
+ workspaceDir?: string,
+ metadata?: Record<string, any>,
+};
+
export type SelectorEngine = {
/**
* Returns the first element matching given selector in the root's subtree.
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index bd7e6e8d09..97052f7479 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -652,8 +652,11 @@ scheme.BrowserContextEvent = tObject({
scheme.BrowserCloseEvent = tOptional(tObject({}));
scheme.BrowserStartServerParams = tObject({
title: tString,
+ host: tOptional(tString),
+ port: tOptional(tInt),
wsPath: tOptional(tString),
workspaceDir: tOptional(tString),
+ metadata: tOptional(tAny),
});
scheme.BrowserStartServerResult = tObject({
wsEndpoint: tOptional(tString),
diff --git a/packages/playwright-core/src/server/browser.ts b/packages/playwright-core/src/server/browser.ts
index c065e68f4d..ebc4ea14aa 100644
--- a/packages/playwright-core/src/server/browser.ts
+++ b/packages/playwright-core/src/server/browser.ts
@@ -174,7 +174,7 @@ export abstract class Browser extends SdkObject {
return video?.artifact;
}
- async startServer(title: string, options: { workspaceDir?: string, wsPath?: string, pipeName?: string }): Promise<{ wsEndpoint?: string, pipeName?: string }> {
+ async startServer(title: string, options: channels.BrowserStartServerOptions): Promise<{ wsEndpoint?: string, pipeName?: string }> {
return await this._server.start(title, options);
}
@@ -219,7 +219,7 @@ export class BrowserServer {
this._browser = browser;
}
- async start(title: string, options: { workspaceDir?: string, wsPath?: string }): Promise<{ wsEndpoint?: string, pipeName?: string }> {
+ async start(title: string, options: channels.BrowserStartServerOptions): Promise<{ wsEndpoint?: string, pipeName?: string }> {
if (this._isStarted)
throw new Error(`Server is already started.`);
this._isStarted = true;
@@ -233,7 +233,7 @@ export class BrowserServer {
if (options.wsPath) {
const path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`;
this._wsServer = new PlaywrightWebSocketServer(this._browser, path);
- result.wsEndpoint = await this._wsServer.listen(0, 'localhost', path);
+ result.wsEndpoint = await this._wsServer.listen(options.port ?? 0, options.host ?? 'localhost', path);
}
const browserInfo: BrowserInfo = {
@@ -247,6 +247,7 @@ export class BrowserServer {
wsEndpoint: result.wsEndpoint,
pipeName: result.pipeName,
workspaceDir: options.workspaceDir,
+ metadata: options.metadata,
});
return result;
}
diff --git a/packages/playwright-core/src/serverRegistry.ts b/packages/playwright-core/src/serverRegistry.ts
index 4aa1410969..1d01068390 100644
--- a/packages/playwright-core/src/serverRegistry.ts
+++ b/packages/playwright-core/src/serverRegistry.ts
@@ -36,6 +36,7 @@ export type EndpointInfo = {
wsEndpoint?: string;
pipeName?: string;
workspaceDir?: string;
+ metadata?: Record<string, any>;
};
export type BrowserDescriptor = EndpointInfo & {
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index 13207e08eb..5344df2987 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -1181,12 +1181,18 @@ export type BrowserContextEvent = {
export type BrowserCloseEvent = {};
export type BrowserStartServerParams = {
title: string,
+ host?: string,
+ port?: number,
wsPath?: string,
workspaceDir?: string,
+ metadata?: any,
};
export type BrowserStartServerOptions = {
+ host?: string,
+ port?: number,
wsPath?: string,
workspaceDir?: string,
+ metadata?: any,
};
export type BrowserStartServerResult = {
wsEndpoint?: string,
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index b8d0e30d73..bbe13e5c33 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -1043,8 +1043,11 @@ Browser:
title: Start server
parameters:
title: string
+ host: string?
+ port: int?
wsPath: string?
workspaceDir: string?
+ metadata: json?
returns:
wsEndpoint: string?
pipeName: string?