androidServerImpl.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.AndroidServerLauncherImpl = void 0;
  6. var _utilsBundle = require("./utilsBundle");
  7. var _utils = require("./utils");
  8. var _playwright = require("./server/playwright");
  9. var _playwrightServer = require("./remote/playwrightServer");
  10. /**
  11. * Copyright (c) Microsoft Corporation.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the 'License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. class AndroidServerLauncherImpl {
  26. async launchServer(options = {}) {
  27. const playwright = (0, _playwright.createPlaywright)({
  28. sdkLanguage: 'javascript',
  29. isServer: true
  30. });
  31. // 1. Pre-connect to the device
  32. let devices = await playwright.android.devices({
  33. host: options.adbHost,
  34. port: options.adbPort,
  35. omitDriverInstall: options.omitDriverInstall
  36. });
  37. if (devices.length === 0) throw new Error('No devices found');
  38. if (options.deviceSerialNumber) {
  39. devices = devices.filter(d => d.serial === options.deviceSerialNumber);
  40. if (devices.length === 0) throw new Error(`No device with serial number '${options.deviceSerialNumber}' not found`);
  41. }
  42. if (devices.length > 1) throw new Error(`More than one device found. Please specify deviceSerialNumber`);
  43. const device = devices[0];
  44. const path = options.wsPath ? options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}` : `/${(0, _utils.createGuid)()}`;
  45. // 2. Start the server
  46. const server = new _playwrightServer.PlaywrightServer({
  47. mode: 'launchServer',
  48. path,
  49. maxConnections: 1,
  50. preLaunchedAndroidDevice: device
  51. });
  52. const wsEndpoint = await server.listen(options.port);
  53. // 3. Return the BrowserServer interface
  54. const browserServer = new _utilsBundle.ws.EventEmitter();
  55. browserServer.wsEndpoint = () => wsEndpoint;
  56. browserServer.close = () => device.close();
  57. browserServer.kill = () => device.close();
  58. device.on('close', () => {
  59. server.close();
  60. browserServer.emit('close');
  61. });
  62. return browserServer;
  63. }
  64. }
  65. exports.AndroidServerLauncherImpl = AndroidServerLauncherImpl;