Skip to content

Commit

Permalink
more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Oct 24, 2022
1 parent 7189e87 commit c12d630
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/src/api/class-android.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>',
// deviceSerialNumber: '<deviceSerialNumber>',
});
const wsEndpoint = browserServer.wsEndpoint();
console.log(wsEndpoint);
Expand Down
4 changes: 1 addition & 3 deletions packages/playwright-core/src/androidServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 1 addition & 3 deletions packages/playwright-core/src/browserServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/client/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Android extends ChannelOwner<channels.AndroidChannel> 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/client/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> 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!);
Expand Down
10 changes: 8 additions & 2 deletions packages/playwright-core/src/remote/playwrightConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>',
* // deviceSerialNumber: '<deviceSerialNumber>',
* });
* const wsEndpoint = browserServer.wsEndpoint();
* console.log(wsEndpoint);
Expand Down
4 changes: 2 additions & 2 deletions tests/android/launch-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit c12d630

Please sign in to comment.