|
| 1 | +import { exec } from 'node:child_process'; |
| 2 | +import { promisify } from 'node:util'; |
| 3 | +import { AndroidPage } from '@midscene/android'; |
| 4 | +const execPromise = promisify(exec); |
| 5 | + |
| 6 | +interface StartAppOptions { |
| 7 | + /** |
| 8 | + * The name of the application package |
| 9 | + */ |
| 10 | + pkg: string; |
| 11 | + /** |
| 12 | + * The name of the main application activity. |
| 13 | + * This or action is required in order to be able to launch an app. |
| 14 | + */ |
| 15 | + activity?: string; |
| 16 | + /** |
| 17 | + * The name of the intent action that will launch the required app. |
| 18 | + * This or activity is required in order to be able to launch an app. |
| 19 | + */ |
| 20 | + action?: string; |
| 21 | + /** |
| 22 | + * If this property is set to `true` |
| 23 | + * and the activity name does not start with '.' then the method |
| 24 | + * will try to add the missing dot and start the activity once more |
| 25 | + * if the first startup try fails. |
| 26 | + * `true` by default. |
| 27 | + */ |
| 28 | + retry?: boolean; |
| 29 | + /** |
| 30 | + * Set it to `true` in order to forcefully |
| 31 | + * stop the activity if it is already running. |
| 32 | + * `true` by default. |
| 33 | + */ |
| 34 | + stopApp?: boolean; |
| 35 | + /** |
| 36 | + * The name of the package to wait to on |
| 37 | + * startup (this only makes sense if this name is |
| 38 | + * different from the one, which is set as `pkg`) |
| 39 | + */ |
| 40 | + waitPkg?: string; |
| 41 | + /** |
| 42 | + * The name of the activity to wait to on |
| 43 | + * startup (this only makes sense if this name is different |
| 44 | + * from the one, which is set as `activity`) |
| 45 | + */ |
| 46 | + waitActivity?: string; |
| 47 | + /** |
| 48 | + * The number of milliseconds to wait until the |
| 49 | + * `waitActivity` is focused |
| 50 | + */ |
| 51 | + waitDuration?: number; |
| 52 | + /** |
| 53 | + * The number of the user profile to start |
| 54 | + * the given activity with. The default OS user profile (usually zero) is used |
| 55 | + * when this property is unset |
| 56 | + */ |
| 57 | + user?: string | number; |
| 58 | + /** |
| 59 | + * If `false` then adb won't wait |
| 60 | + * for the started activity to return the control. |
| 61 | + * `true` by default. |
| 62 | + */ |
| 63 | + waitForLaunch?: boolean; |
| 64 | + category?: string; |
| 65 | + flags?: string; |
| 66 | + optionalIntentArguments?: string; |
| 67 | +} |
| 68 | + |
| 69 | +interface LaunchOptions { |
| 70 | + deviceId?: string; |
| 71 | + uri?: string; |
| 72 | + app?: StartAppOptions; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Get all connected Android device IDs |
| 77 | + * @returns List of device IDs |
| 78 | + * @throws Error when unable to retrieve device list |
| 79 | + */ |
| 80 | +export async function getConnectedDevices(): Promise<string[]> { |
| 81 | + try { |
| 82 | + const { stdout } = await execPromise('adb devices'); |
| 83 | + const devices = stdout |
| 84 | + .split('\n') |
| 85 | + .slice(1) // Skip the first line "List of devices attached" |
| 86 | + .map((line) => { |
| 87 | + const [id, status] = line.split('\t'); |
| 88 | + return { id, status }; |
| 89 | + }) |
| 90 | + .filter(({ id, status }) => id && status && status.trim() === 'device') |
| 91 | + .map(({ id }) => id); |
| 92 | + |
| 93 | + return devices; |
| 94 | + } catch (error) { |
| 95 | + console.error('Failed to get device list:', error); |
| 96 | + throw new Error('Unable to get connected Android device list'); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Verify if the device is accessible |
| 102 | + * @param deviceId Device ID |
| 103 | + * @returns true if the device is accessible, false otherwise |
| 104 | + */ |
| 105 | +export async function isDeviceAccessible(deviceId: string): Promise<boolean> { |
| 106 | + try { |
| 107 | + await execPromise(`adb -s ${deviceId} shell echo "Device is ready"`); |
| 108 | + return true; |
| 109 | + } catch { |
| 110 | + return false; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Launch Android page |
| 116 | + * @param opt Launch options |
| 117 | + * @returns AndroidPage instance |
| 118 | + * @throws Error when no available device is found |
| 119 | + */ |
| 120 | +export async function launchPage(opt: LaunchOptions): Promise<AndroidPage> { |
| 121 | + // If device ID is provided, use it directly |
| 122 | + let deviceId = opt.deviceId; |
| 123 | + |
| 124 | + if (!deviceId) { |
| 125 | + // Get all connected devices |
| 126 | + const devices = await getConnectedDevices(); |
| 127 | + |
| 128 | + if (devices.length === 0) { |
| 129 | + throw new Error('No available Android devices found'); |
| 130 | + } |
| 131 | + |
| 132 | + if (devices.length > 1) { |
| 133 | + console.warn( |
| 134 | + `Multiple devices detected: ${devices.join(', ')}. Using the first device: ${devices[0]}`, |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + // Use the first available device |
| 139 | + deviceId = devices[0]; |
| 140 | + } |
| 141 | + |
| 142 | + // Verify if the device is accessible |
| 143 | + const isAccessible = await isDeviceAccessible(deviceId); |
| 144 | + if (!isAccessible) { |
| 145 | + throw new Error( |
| 146 | + `Device ${deviceId} is not accessible, please check the connection status`, |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + const androidPage = new AndroidPage({ |
| 151 | + deviceId, |
| 152 | + }); |
| 153 | + |
| 154 | + const adb = await androidPage.getAdb(); |
| 155 | + |
| 156 | + // handle URI (if provided), support app page and web page |
| 157 | + if (opt.uri) { |
| 158 | + try { |
| 159 | + await adb.startUri(opt.uri); |
| 160 | + } catch (error) { |
| 161 | + console.error(`Error starting URI: ${error}`); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (opt.app) { |
| 166 | + try { |
| 167 | + await adb.startApp(opt.app); |
| 168 | + } catch (error) { |
| 169 | + console.error(`Error starting app: ${error}`); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return androidPage; |
| 174 | +} |
0 commit comments