androidDispatcher.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.AndroidSocketDispatcher = exports.AndroidDispatcher = exports.AndroidDeviceDispatcher = void 0;
  6. var _dispatcher = require("./dispatcher");
  7. var _android = require("../android/android");
  8. var _browserContextDispatcher = require("./browserContextDispatcher");
  9. /**
  10. * Copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the 'License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. class AndroidDispatcher extends _dispatcher.Dispatcher {
  25. constructor(scope, android) {
  26. super(scope, android, 'Android', {});
  27. this._type_Android = true;
  28. }
  29. async devices(params) {
  30. const devices = await this._object.devices(params);
  31. return {
  32. devices: devices.map(d => AndroidDeviceDispatcher.from(this, d))
  33. };
  34. }
  35. async setDefaultTimeoutNoReply(params) {
  36. this._object.setDefaultTimeout(params.timeout);
  37. }
  38. }
  39. exports.AndroidDispatcher = AndroidDispatcher;
  40. class AndroidDeviceDispatcher extends _dispatcher.Dispatcher {
  41. static from(scope, device) {
  42. const result = (0, _dispatcher.existingDispatcher)(device);
  43. return result || new AndroidDeviceDispatcher(scope, device);
  44. }
  45. constructor(scope, device) {
  46. super(scope, device, 'AndroidDevice', {
  47. model: device.model,
  48. serial: device.serial
  49. });
  50. this._type_EventTarget = true;
  51. this._type_AndroidDevice = true;
  52. for (const webView of device.webViews()) this._dispatchEvent('webViewAdded', {
  53. webView
  54. });
  55. this.addObjectListener(_android.AndroidDevice.Events.WebViewAdded, webView => this._dispatchEvent('webViewAdded', {
  56. webView
  57. }));
  58. this.addObjectListener(_android.AndroidDevice.Events.WebViewRemoved, socketName => this._dispatchEvent('webViewRemoved', {
  59. socketName
  60. }));
  61. this.addObjectListener(_android.AndroidDevice.Events.Close, socketName => this._dispatchEvent('close'));
  62. }
  63. async wait(params) {
  64. await this._object.send('wait', params);
  65. }
  66. async fill(params) {
  67. await this._object.send('click', {
  68. selector: params.selector
  69. });
  70. await this._object.send('fill', params);
  71. }
  72. async tap(params) {
  73. await this._object.send('click', params);
  74. }
  75. async drag(params) {
  76. await this._object.send('drag', params);
  77. }
  78. async fling(params) {
  79. await this._object.send('fling', params);
  80. }
  81. async longTap(params) {
  82. await this._object.send('longClick', params);
  83. }
  84. async pinchClose(params) {
  85. await this._object.send('pinchClose', params);
  86. }
  87. async pinchOpen(params) {
  88. await this._object.send('pinchOpen', params);
  89. }
  90. async scroll(params) {
  91. await this._object.send('scroll', params);
  92. }
  93. async swipe(params) {
  94. await this._object.send('swipe', params);
  95. }
  96. async info(params) {
  97. return {
  98. info: await this._object.send('info', params)
  99. };
  100. }
  101. async inputType(params) {
  102. const text = params.text;
  103. const keyCodes = [];
  104. for (let i = 0; i < text.length; ++i) {
  105. const code = keyMap.get(text[i].toUpperCase());
  106. if (code === undefined) throw new Error('No mapping for ' + text[i] + ' found');
  107. keyCodes.push(code);
  108. }
  109. await Promise.all(keyCodes.map(keyCode => this._object.send('inputPress', {
  110. keyCode
  111. })));
  112. }
  113. async inputPress(params) {
  114. if (!keyMap.has(params.key)) throw new Error('Unknown key: ' + params.key);
  115. await this._object.send('inputPress', {
  116. keyCode: keyMap.get(params.key)
  117. });
  118. }
  119. async inputTap(params) {
  120. await this._object.send('inputClick', params);
  121. }
  122. async inputSwipe(params) {
  123. await this._object.send('inputSwipe', params);
  124. }
  125. async inputDrag(params) {
  126. await this._object.send('inputDrag', params);
  127. }
  128. async screenshot(params) {
  129. return {
  130. binary: await this._object.screenshot()
  131. };
  132. }
  133. async shell(params) {
  134. return {
  135. result: await this._object.shell(params.command)
  136. };
  137. }
  138. async open(params, metadata) {
  139. const socket = await this._object.open(params.command);
  140. return {
  141. socket: new AndroidSocketDispatcher(this, socket)
  142. };
  143. }
  144. async installApk(params) {
  145. await this._object.installApk(params.file, {
  146. args: params.args
  147. });
  148. }
  149. async push(params) {
  150. await this._object.push(params.file, params.path, params.mode);
  151. }
  152. async launchBrowser(params) {
  153. const context = await this._object.launchBrowser(params.pkg, params);
  154. return {
  155. context: new _browserContextDispatcher.BrowserContextDispatcher(this, context)
  156. };
  157. }
  158. async close(params) {
  159. await this._object.close();
  160. }
  161. async setDefaultTimeoutNoReply(params) {
  162. this._object.setDefaultTimeout(params.timeout);
  163. }
  164. async connectToWebView(params) {
  165. return {
  166. context: new _browserContextDispatcher.BrowserContextDispatcher(this, await this._object.connectToWebView(params.socketName))
  167. };
  168. }
  169. }
  170. exports.AndroidDeviceDispatcher = AndroidDeviceDispatcher;
  171. class AndroidSocketDispatcher extends _dispatcher.Dispatcher {
  172. constructor(scope, socket) {
  173. super(scope, socket, 'AndroidSocket', {});
  174. this._type_AndroidSocket = true;
  175. this.addObjectListener('data', data => this._dispatchEvent('data', {
  176. data
  177. }));
  178. this.addObjectListener('close', () => {
  179. this._dispatchEvent('close');
  180. this._dispose();
  181. });
  182. }
  183. async write(params, metadata) {
  184. await this._object.write(params.data);
  185. }
  186. async close(params, metadata) {
  187. this._object.close();
  188. }
  189. }
  190. exports.AndroidSocketDispatcher = AndroidSocketDispatcher;
  191. const keyMap = new Map([['Unknown', 0], ['SoftLeft', 1], ['SoftRight', 2], ['Home', 3], ['Back', 4], ['Call', 5], ['EndCall', 6], ['0', 7], ['1', 8], ['2', 9], ['3', 10], ['4', 11], ['5', 12], ['6', 13], ['7', 14], ['8', 15], ['9', 16], ['Star', 17], ['*', 17], ['Pound', 18], ['#', 18], ['DialUp', 19], ['DialDown', 20], ['DialLeft', 21], ['DialRight', 22], ['DialCenter', 23], ['VolumeUp', 24], ['VolumeDown', 25], ['Power', 26], ['Camera', 27], ['Clear', 28], ['A', 29], ['B', 30], ['C', 31], ['D', 32], ['E', 33], ['F', 34], ['G', 35], ['H', 36], ['I', 37], ['J', 38], ['K', 39], ['L', 40], ['M', 41], ['N', 42], ['O', 43], ['P', 44], ['Q', 45], ['R', 46], ['S', 47], ['T', 48], ['U', 49], ['V', 50], ['W', 51], ['X', 52], ['Y', 53], ['Z', 54], ['Comma', 55], [',', 55], ['Period', 56], ['.', 56], ['AltLeft', 57], ['AltRight', 58], ['ShiftLeft', 59], ['ShiftRight', 60], ['Tab', 61], ['\t', 61], ['Space', 62], [' ', 62], ['Sym', 63], ['Explorer', 64], ['Envelop', 65], ['Enter', 66], ['Del', 67], ['Grave', 68], ['Minus', 69], ['-', 69], ['Equals', 70], ['=', 70], ['LeftBracket', 71], ['(', 71], ['RightBracket', 72], [')', 72], ['Backslash', 73], ['\\', 73], ['Semicolon', 74], [';', 74], ['Apostrophe', 75], ['`', 75], ['Slash', 76], ['/', 76], ['At', 77], ['@', 77], ['Num', 78], ['HeadsetHook', 79], ['Focus', 80], ['Plus', 81], ['Menu', 82], ['Notification', 83], ['Search', 84], ['AppSwitch', 187], ['Assist', 219], ['Cut', 277], ['Copy', 278], ['Paste', 279]]);