|
| 1 | +/** |
| 2 | + * Tool to install and launch WebDriverAgent (WDA) on a booted iOS simulator |
| 3 | + */ |
| 4 | +import { z } from 'zod'; |
| 5 | +import { exec } from 'child_process'; |
| 6 | +import { promisify } from 'util'; |
| 7 | +import path from 'path'; |
| 8 | +import fs from 'fs'; |
| 9 | +import os from 'os'; |
| 10 | + |
| 11 | +const execAsync = promisify(exec); |
| 12 | + |
| 13 | +function cachePath(folder: string): string { |
| 14 | + return path.join(os.homedir(), '.cache', 'appium-mcp', folder); |
| 15 | +} |
| 16 | + |
| 17 | +async function getLatestWDAVersion(): Promise<string> { |
| 18 | + // Scan the cache directory to find the latest version |
| 19 | + const wdaCacheDir = cachePath('wda'); |
| 20 | + if (!fs.existsSync(wdaCacheDir)) { |
| 21 | + throw new Error('No WDA cache found. Please run setup_wda first.'); |
| 22 | + } |
| 23 | + |
| 24 | + const versions = fs |
| 25 | + .readdirSync(wdaCacheDir) |
| 26 | + .filter(dir => fs.statSync(path.join(wdaCacheDir, dir)).isDirectory()) |
| 27 | + .sort((a, b) => { |
| 28 | + // Simple version comparison - you might want to use semver for more complex versions |
| 29 | + return b.localeCompare(a, undefined, { numeric: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + if (versions.length === 0) { |
| 33 | + throw new Error( |
| 34 | + 'No WDA versions found in cache. Please run setup_wda first.' |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return versions[0]; |
| 39 | +} |
| 40 | + |
| 41 | +async function getBootedSimulators(): Promise<string[]> { |
| 42 | + try { |
| 43 | + const { stdout } = await execAsync('xcrun simctl list devices --json'); |
| 44 | + const data = JSON.parse(stdout); |
| 45 | + const bootedSimulators: string[] = []; |
| 46 | + |
| 47 | + for (const [runtime, devices] of Object.entries(data.devices)) { |
| 48 | + if (Array.isArray(devices)) { |
| 49 | + for (const device of devices as any[]) { |
| 50 | + if (device.state === 'Booted') { |
| 51 | + bootedSimulators.push(device.udid); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return bootedSimulators; |
| 58 | + } catch (error) { |
| 59 | + throw new Error(`Failed to list simulators: ${error}`); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async function installAppOnSimulator( |
| 64 | + appPath: string, |
| 65 | + simulatorUdid: string |
| 66 | +): Promise<void> { |
| 67 | + try { |
| 68 | + await execAsync(`xcrun simctl install "${simulatorUdid}" "${appPath}"`); |
| 69 | + } catch (error) { |
| 70 | + throw new Error( |
| 71 | + `Failed to install app on simulator ${simulatorUdid}: ${error}` |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function launchAppOnSimulator( |
| 77 | + bundleId: string, |
| 78 | + simulatorUdid: string |
| 79 | +): Promise<void> { |
| 80 | + try { |
| 81 | + await execAsync(`xcrun simctl launch "${simulatorUdid}" "${bundleId}"`); |
| 82 | + } catch (error) { |
| 83 | + throw new Error( |
| 84 | + `Failed to launch app on simulator ${simulatorUdid}: ${error}` |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +async function getAppBundleId(appPath: string): Promise<string> { |
| 90 | + try { |
| 91 | + const { stdout } = await execAsync( |
| 92 | + `/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "${path.join(appPath, 'Info.plist')}"` |
| 93 | + ); |
| 94 | + return stdout.trim(); |
| 95 | + } catch (error) { |
| 96 | + throw new Error(`Failed to get bundle ID for app at ${appPath}: ${error}`); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function isWDAInstalled(simulatorUdid: string): Promise<boolean> { |
| 101 | + try { |
| 102 | + const { stdout } = await execAsync( |
| 103 | + `xcrun simctl listapps "${simulatorUdid}" --json` |
| 104 | + ); |
| 105 | + const data = JSON.parse(stdout); |
| 106 | + |
| 107 | + // Check if any app has a bundle ID that looks like WDA |
| 108 | + for (const [bundleId, appInfo] of Object.entries(data)) { |
| 109 | + if ( |
| 110 | + bundleId.includes('WebDriverAgentRunner') || |
| 111 | + (appInfo as any)?.CFBundleName?.includes('WebDriverAgent') |
| 112 | + ) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + return false; |
| 117 | + } catch (error) { |
| 118 | + // If we can't check, assume it's not installed |
| 119 | + return false; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +async function isWDARunning(simulatorUdid: string): Promise<boolean> { |
| 124 | + try { |
| 125 | + const { stdout } = await execAsync( |
| 126 | + `xcrun simctl listapps "${simulatorUdid}" --json` |
| 127 | + ); |
| 128 | + const data = JSON.parse(stdout); |
| 129 | + |
| 130 | + // Check if WDA is running |
| 131 | + for (const [bundleId, appInfo] of Object.entries(data)) { |
| 132 | + if ( |
| 133 | + bundleId.includes('WebDriverAgentRunner') && |
| 134 | + (appInfo as any)?.ApplicationType === 'User' |
| 135 | + ) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + return false; |
| 140 | + } catch (error) { |
| 141 | + return false; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +export default function installWDA(server: any): void { |
| 146 | + server.addTool({ |
| 147 | + name: 'install_wda', |
| 148 | + description: |
| 149 | + 'Install and launch the WebDriverAgent (WDA) app on a booted iOS simulator using the app path from setup_wda. This tool requires WDA to be already set up using setup_wda and at least one simulator to be booted.', |
| 150 | + parameters: z.object({ |
| 151 | + simulatorUdid: z |
| 152 | + .string() |
| 153 | + .optional() |
| 154 | + .describe( |
| 155 | + 'The UDID of the simulator to install WDA on. If not provided, will use the first booted simulator found.' |
| 156 | + ), |
| 157 | + appPath: z |
| 158 | + .string() |
| 159 | + .optional() |
| 160 | + .describe( |
| 161 | + 'The path to the WDA app bundle (.app file) that be generated by setup_wda tool. If not provided, will try to find the latest cached WDA app.' |
| 162 | + ), |
| 163 | + }), |
| 164 | + annotations: { |
| 165 | + readOnlyHint: false, |
| 166 | + openWorldHint: false, |
| 167 | + }, |
| 168 | + execute: async (args: any, context: any): Promise<any> => { |
| 169 | + try { |
| 170 | + const { simulatorUdid, appPath: providedAppPath } = args; |
| 171 | + |
| 172 | + // Verify it's a macOS system |
| 173 | + if (process.platform !== 'darwin') { |
| 174 | + throw new Error( |
| 175 | + 'WDA installation is only supported on macOS systems' |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + // Determine WDA app path |
| 180 | + let appPath: string; |
| 181 | + if (providedAppPath) { |
| 182 | + appPath = providedAppPath; |
| 183 | + } else { |
| 184 | + // Try to find the latest cached WDA app |
| 185 | + const version = await getLatestWDAVersion(); |
| 186 | + const extractDir = cachePath(`wda/${version}/extracted`); |
| 187 | + appPath = path.join(extractDir, 'WebDriverAgentRunner-Runner.app'); |
| 188 | + } |
| 189 | + |
| 190 | + // Verify WDA app exists |
| 191 | + if (!fs.existsSync(appPath)) { |
| 192 | + throw new Error( |
| 193 | + `WDA app not found at ${appPath}. Please run setup_wda first to download and cache WDA, or provide a valid appPath.` |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + // Get booted simulators |
| 198 | + const bootedSimulators = await getBootedSimulators(); |
| 199 | + if (bootedSimulators.length === 0) { |
| 200 | + throw new Error( |
| 201 | + 'No booted simulators found. Please boot a simulator first using boot_simulator tool.' |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + // Determine target simulator |
| 206 | + const targetSimulator = simulatorUdid || bootedSimulators[0]; |
| 207 | + |
| 208 | + if (!bootedSimulators.includes(targetSimulator)) { |
| 209 | + throw new Error( |
| 210 | + `Simulator ${targetSimulator} is not booted. Available booted simulators: ${bootedSimulators.join(', ')}` |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + console.log( |
| 215 | + `Installing WDA from ${appPath} on simulator ${targetSimulator}...` |
| 216 | + ); |
| 217 | + |
| 218 | + // Check if WDA is already installed and running |
| 219 | + const isInstalled = await isWDAInstalled(targetSimulator); |
| 220 | + const isRunning = await isWDARunning(targetSimulator); |
| 221 | + |
| 222 | + if (isRunning) { |
| 223 | + return { |
| 224 | + content: [ |
| 225 | + { |
| 226 | + type: 'text', |
| 227 | + text: `✅ WebDriverAgent is already running on simulator ${targetSimulator}!\n\nSimulator: ${targetSimulator}\nApp Path: ${appPath}\nStatus: Running\n\n🚀 WDA is ready to accept connections from Appium.`, |
| 228 | + }, |
| 229 | + ], |
| 230 | + }; |
| 231 | + } |
| 232 | + |
| 233 | + // Install the app (only if not already installed) |
| 234 | + if (!isInstalled) { |
| 235 | + await installAppOnSimulator(appPath, targetSimulator); |
| 236 | + console.log('WDA app installed successfully'); |
| 237 | + } else { |
| 238 | + console.log('WDA app already installed, skipping installation'); |
| 239 | + } |
| 240 | + |
| 241 | + // Get bundle ID and launch the app |
| 242 | + const bundleId = await getAppBundleId(appPath); |
| 243 | + console.log(`Launching WDA with bundle ID: ${bundleId}`); |
| 244 | + await launchAppOnSimulator(bundleId, targetSimulator); |
| 245 | + |
| 246 | + return { |
| 247 | + content: [ |
| 248 | + { |
| 249 | + type: 'text', |
| 250 | + text: `✅ WebDriverAgent installed and launched successfully!\n\nSimulator: ${targetSimulator}\nBundle ID: ${bundleId}\nApp Path: ${appPath}\nInstallation: ${isInstalled ? 'Skipped (already installed)' : 'Completed'}\n\n🚀 WDA is now running on the simulator and ready to accept connections from Appium.\n\nNote: The WDA app should be visible on the simulator screen. You can now create an Appium session.`, |
| 251 | + }, |
| 252 | + ], |
| 253 | + }; |
| 254 | + } catch (error: any) { |
| 255 | + console.error('Error installing WDA:', error); |
| 256 | + throw new Error(`Failed to install WebDriverAgent: ${error.message}`); |
| 257 | + } |
| 258 | + }, |
| 259 | + }); |
| 260 | +} |
0 commit comments