From c12d630dbc2cf40a55c9faeafb1813d6c3c15635 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 24 Oct 2022 15:06:29 -0700 Subject: [PATCH] more stuff --- docs/src/api/class-android.md | 2 +- packages/playwright-core/src/androidServerImpl.ts | 4 +--- packages/playwright-core/src/browserServerImpl.ts | 4 +--- packages/playwright-core/src/client/android.ts | 2 +- packages/playwright-core/src/client/browserType.ts | 2 +- .../playwright-core/src/remote/playwrightConnection.ts | 10 ++++++++-- packages/playwright-core/types/types.d.ts | 2 +- tests/android/launch-server.spec.ts | 4 ++-- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/src/api/class-android.md b/docs/src/api/class-android.md index f51d647d09fcc6..7ba985b3805239 100644 --- a/docs/src/api/class-android.md +++ b/docs/src/api/class-android.md @@ -150,7 +150,7 @@ const { _android } = require('playwright'); (async () => { const browserServer = await _android.launchServer({ // If you have multiple devices connected and want to use a specific one. - deviceSerialNumber: '', + // deviceSerialNumber: '', }); const wsEndpoint = browserServer.wsEndpoint(); console.log(wsEndpoint); diff --git a/packages/playwright-core/src/androidServerImpl.ts b/packages/playwright-core/src/androidServerImpl.ts index 7c886a73dcc3df..65643ae53a7048 100644 --- a/packages/playwright-core/src/androidServerImpl.ts +++ b/packages/playwright-core/src/androidServerImpl.ts @@ -46,9 +46,7 @@ export class AndroidServerLauncherImpl { const device = devices[0]; - let path = `/${createGuid()}`; - if (options.wsPath) - path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`; + const path = options.wsPath ? (options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`) : `/${createGuid()}`; // 2. Start the server const server = new PlaywrightServer({ path, maxConnections: 1, enableSocksProxy: false, preLaunchedAndroidDevice: device }); diff --git a/packages/playwright-core/src/browserServerImpl.ts b/packages/playwright-core/src/browserServerImpl.ts index 8b6d4314ff7678..a602bda7a8e751 100644 --- a/packages/playwright-core/src/browserServerImpl.ts +++ b/packages/playwright-core/src/browserServerImpl.ts @@ -49,9 +49,7 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher { throw e; }); - let path = `/${createGuid()}`; - if (options.wsPath) - path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`; + const path = options.wsPath ? (options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`) : `/${createGuid()}`; // 2. Start the server const server = new PlaywrightServer({ path, maxConnections: Infinity, enableSocksProxy: false, preLaunchedBrowser: browser }); diff --git a/packages/playwright-core/src/client/android.ts b/packages/playwright-core/src/client/android.ts index e7d01b9b09a5c3..3d8e83b8315f48 100644 --- a/packages/playwright-core/src/client/android.ts +++ b/packages/playwright-core/src/client/android.ts @@ -97,7 +97,7 @@ export class Android extends ChannelOwner implements ap const playwright = await connection!.initializePlaywright(); if (!playwright._initializer.preConnectedAndroidDevice) { closePipe(); - throw new Error('Malformed endpoint. Did you use launchServer method?'); + throw new Error('Malformed endpoint. Did you use Android.launchServer method?'); } device = AndroidDevice.from(playwright._initializer.preConnectedAndroidDevice!); device._shouldCloseConnectionOnClose = true; diff --git a/packages/playwright-core/src/client/browserType.ts b/packages/playwright-core/src/client/browserType.ts index 131ce177a8cb25..27dd6aa5cf7d38 100644 --- a/packages/playwright-core/src/client/browserType.ts +++ b/packages/playwright-core/src/client/browserType.ts @@ -188,7 +188,7 @@ export class BrowserType extends ChannelOwner imple const playwright = await connection!.initializePlaywright(); if (!playwright._initializer.preLaunchedBrowser) { closePipe(); - throw new Error('Malformed endpoint. Did you use launchServer method?'); + throw new Error('Malformed endpoint. Did you use BrowserType.launchServer method?'); } playwright._setSelectors(this._playwright.selectors); browser = Browser.from(playwright._initializer.preLaunchedBrowser!); diff --git a/packages/playwright-core/src/remote/playwrightConnection.ts b/packages/playwright-core/src/remote/playwrightConnection.ts index e6942960abf451..4e38a59bb99350 100644 --- a/packages/playwright-core/src/remote/playwrightConnection.ts +++ b/packages/playwright-core/src/remote/playwrightConnection.ts @@ -23,7 +23,7 @@ import { gracefullyCloseAll } from '../utils/processLauncher'; import { SocksProxy } from '../common/socksProxy'; import { assert } from '../utils'; import type { LaunchOptions } from '../server/types'; -import type { AndroidDevice } from '../server/android/android'; +import { AndroidDevice } from '../server/android/android'; import { DebugControllerDispatcher } from '../server/dispatchers/debugControllerDispatcher'; export type ClientType = 'controller' | 'playwright' | 'launch-browser' | 'reuse-browser' | 'pre-launched-browser'; @@ -74,7 +74,7 @@ export class PlaywrightConnection { }); ws.on('close', () => this._onDisconnect()); - ws.on('error', error => this._onDisconnect(error)); + ws.on('error', (error: Error) => this._onDisconnect(error)); if (clientType === 'controller') { this._root = this._initDebugControllerMode(); @@ -134,6 +134,12 @@ export class PlaywrightConnection { }); } const androidDevice = this._preLaunched.androidDevice; + if (androidDevice) { + androidDevice.on(AndroidDevice.Events.Closed, () => { + // Underlying browser did close for some reason - force disconnect the client. + this.close({ code: 1001, reason: 'Android device disconnected' }); + }); + } const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, undefined, browser, androidDevice); // In pre-launched mode, keep only the pre-launched browser. for (const b of playwright.allBrowsers()) { diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index f98a66e09f1d35..b7c30707212e13 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -12240,7 +12240,7 @@ export interface Android { * (async () => { * const browserServer = await _android.launchServer({ * // If you have multiple devices connected and want to use a specific one. - * deviceSerialNumber: '', + * // deviceSerialNumber: '', * }); * const wsEndpoint = browserServer.wsEndpoint(); * console.log(wsEndpoint); diff --git a/tests/android/launch-server.spec.ts b/tests/android/launch-server.spec.ts index a5969c275f60bd..6ebd6dbc0efbf9 100644 --- a/tests/android/launch-server.spec.ts +++ b/tests/android/launch-server.spec.ts @@ -70,7 +70,7 @@ test('android.launchServer BrowserServer.close() will disconnect the device', as try { const device = await playwright._android.connect(browserServer.wsEndpoint()); await browserServer.close(); - await expect(device.shell('echo 123')).rejects.toThrow('androidDevice.shell: Device is closed'); + await expect(device.shell('echo 123')).rejects.toThrow('androidDevice.shell: Browser has been closed'); } finally { await browserServer.close(); } @@ -81,7 +81,7 @@ test('android.launchServer BrowserServer.kill() will disconnect the device', as try { const device = await playwright._android.connect(browserServer.wsEndpoint()); await browserServer.kill(); - await expect(device.shell('echo 123')).rejects.toThrow('androidDevice.shell: Device is closed'); + await expect(device.shell('echo 123')).rejects.toThrow('androidDevice.shell: Browser has been closed'); } finally { await browserServer.close(); }