MCPcopy
hub / github.com/mobile-next/mobile-mcp / Simctl

Class Simctl

src/iphone-simulator.ts:33–283  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31const MAX_BUFFER_SIZE = 1024 * 1024 * 8;
32
33export class Simctl implements Robot {
34
35 constructor(private readonly simulatorUuid: string) {}
36
37 private async isWdaInstalled(): Promise<boolean> {
38 const apps = await this.listApps();
39 return apps.map(app => app.packageName).includes("com.facebook.WebDriverAgentRunner.xctrunner");
40 }
41
42 private async startWda(): Promise<void> {
43 if (!(await this.isWdaInstalled())) {
44 // wda is not even installed, won't attempt to start it
45 return;
46 }
47
48 trace("Starting WebDriverAgent");
49 const webdriverPackageName = "com.facebook.WebDriverAgentRunner.xctrunner";
50 this.simctl("launch", this.simulatorUuid, webdriverPackageName);
51
52 // now we wait for wda to have a successful status
53 const wda = new WebDriverAgent("localhost", WDA_PORT);
54
55 // wait up to 10 seconds for wda to start
56 const timeout = +new Date() + 10 * 1000;
57 while (+new Date() < timeout) {
58 // cross fingers and see if wda is already running
59 if (await wda.isRunning()) {
60 trace("WebDriverAgent is now running");
61 return;
62 }
63
64 // wait 100ms before trying again
65 await new Promise(resolve => setTimeout(resolve, 100));
66 }
67
68 trace("Could not start WebDriverAgent in time, giving up");
69 }
70
71 private async wda(): Promise<WebDriverAgent> {
72 const wda = new WebDriverAgent("localhost", WDA_PORT);
73
74 if (!(await wda.isRunning())) {
75 await this.startWda();
76 if (!(await wda.isRunning())) {
77 throw new ActionableError("WebDriverAgent is not running on simulator, please see https://github.com/mobile-next/mobile-mcp/wiki/");
78 }
79
80 // was successfully started
81 }
82
83 return wda;
84 }
85
86 private simctl(...args: string[]): Buffer {
87 return execFileSync("xcrun", ["simctl", ...args], {
88 timeout: TIMEOUT,
89 maxBuffer: MAX_BUFFER_SIZE,
90 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected