Skip to content

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

feat(run-server): expose --host (#28723)

Max Schmittb88b194e9f15
diff --git a/packages/playwright-core/src/cli/driver.ts b/packages/playwright-core/src/cli/driver.ts
index cc3c5dab25..245c733483 100644
--- a/packages/playwright-core/src/cli/driver.ts
+++ b/packages/playwright-core/src/cli/driver.ts
@@ -53,6 +53,7 @@ export function runDriver() {
export type RunServerOptions = {
port?: number,
+ host?: string,
path?: string,
extension?: boolean,
maxConnections?: number,
@@ -63,12 +64,13 @@ export type RunServerOptions = {
export async function runServer(options: RunServerOptions) {
const {
port,
+ host,
path = '/',
maxConnections = Infinity,
extension,
} = options;
const server = new PlaywrightServer({ mode: extension ? 'extension' : 'default', path, maxConnections });
- const wsEndpoint = await server.listen(port);
+ const wsEndpoint = await server.listen(port, host);
process.on('exit', () => server.close().catch(console.error));
console.log('Listening on ' + wsEndpoint);
process.stdin.on('close', () => gracefullyProcessExitDoNotHang(0));
diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.ts
index dc3825feca..6e67df3f38 100644
--- a/packages/playwright-core/src/cli/program.ts
+++ b/packages/playwright-core/src/cli/program.ts
@@ -257,12 +257,14 @@ program
program
.command('run-server', { hidden: true })
.option('--port <port>', 'Server port')
+ .option('--host <host>', 'Server host')
.option('--path <path>', 'Endpoint Path', '/')
.option('--max-clients <maxClients>', 'Maximum clients')
.option('--mode <mode>', 'Server mode, either "default" or "extension"')
.action(function(options) {
runServer({
port: options.port ? +options.port : undefined,
+ host: options.host,
path: options.path,
maxConnections: options.maxClients ? +options.maxClients : Infinity,
extension: options.mode === 'extension' || !!process.env.PW_EXTENSION_MODE,
diff --git a/packages/playwright-core/src/remote/playwrightServer.ts b/packages/playwright-core/src/remote/playwrightServer.ts
index cfccdb3fcf..664a5fce77 100644
--- a/packages/playwright-core/src/remote/playwrightServer.ts
+++ b/packages/playwright-core/src/remote/playwrightServer.ts
@@ -56,7 +56,7 @@ export class PlaywrightServer {
this._preLaunchedPlaywright = options.preLaunchedAndroidDevice._android.attribution.playwright;
}
- async listen(port: number = 0): Promise<string> {
+ async listen(port: number = 0, hostname?: string): Promise<string> {
debugLogger.log('server', `Server started at ${new Date()}`);
const server = createHttpServer((request: http.IncomingMessage, response: http.ServerResponse) => {
@@ -73,13 +73,13 @@ export class PlaywrightServer {
this._server = server;
const wsEndpoint = await new Promise<string>((resolve, reject) => {
- server.listen(port, () => {
+ server.listen(port, hostname, () => {
const address = server.address();
if (!address) {
reject(new Error('Could not bind server socket'));
return;
}
- const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://127.0.0.1:${address.port}${this._options.path}`;
+ const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://${hostname || 'localhost'}:${address.port}${this._options.path}`;
resolve(wsEndpoint);
}).on('error', reject);
});