Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

feat: support physical ios devices through go-ios (#17)

Gil Megidish337275db5d7e
diff --git a/README.md b/README.md
index fa0c07a..ec3a25e 100644
--- a/README.md
+++ b/README.md
@@ -109,16 +109,10 @@ On iOS, you'll need Xcode and to run the Simulator before using Mobile MCP with
- `xcrun simctl list`
- `xcrun simctl boot "iPhone 16"`
-
# Mobile Commands and interaction tools
The commands and tools support both accessibility-based locators (preferred) and coordinate-based inputs, giving you flexibility when accessibility/automation IDs are missing for reliable and seemless automation.
-## mobile_install_app
-- **Description:** Installs an app onto the device/emulator
-- **Parameters:**
- - `appPath` (string): Path or URL to the app file (e.g., .apk for Android, .ipa/.app for iOS)
-
## mobile_list_apps
- **Description:** List all the installed apps on the device
- **Parameters:**
@@ -172,8 +166,6 @@ The commands and tools support both accessibility-based locators (preferred) and
## mobile_type_text
- **Description:** Types text into a focused UI element (e.g., TextField, SearchField)
- **Parameters:**
- - `element` (string): Human-readable element description
- - `ref` (string): Accessibility/automation ID of the element
- `text` (string): Text to type
- `submit` (boolean): Whether to press Enter/Return after typing
@@ -200,22 +192,12 @@ The commands and tools support both accessibility-based locators (preferred) and
## mobile_take_screenshot
- **Description:** Captures a screenshot of the current device screen
-- **Parameters:**
- - `raw` (boolean): Return a lossless image if true; otherwise, compressed by default
+- **Parameters:** None
## mobile_get_source
- **Description:** Fetches the current device UI structure (accessibility snapshot) (xml format)
- **Parameters:** None
-## mobile_wait
-- **Description:** Waits for a specified time
-- **Parameters:**
- - `time` (number): Time to wait in seconds (capped at 10 seconds)
-
-## mobile_close_session
-- **Description:** Closes the current device session
-- **Parameters:** None
-
# Thanks to all contributors ❤️
diff --git a/src/android.ts b/src/android.ts
index b3a140c..28beedf 100644
--- a/src/android.ts
+++ b/src/android.ts
@@ -1,7 +1,9 @@
-import { execFileSync, execSync } from "child_process";
+import path from "path";
+import { execFileSync } from "child_process";
+
import * as xml from "fast-xml-parser";
+
import { Bounds, Button, Dimensions, ElementCoordinates, Robot, SwipeDirection } from "./robot";
-import path from "path";
interface UiAutomatorXmlNode {
node: UiAutomatorXmlNode[];
@@ -16,10 +18,40 @@ interface UiAutomatorXml {
};
}
+const getAdbPath = (): string => {
+ let executable = "adb";
+ if (process.env.ANDROID_HOME) {
+ executable = path.join(process.env.ANDROID_HOME, "platform-tools", "adb");
+ }
+
+ return executable;
+};
+
+const BUTTON_MAP: Record<Button, string> = {
+ "BACK": "KEYCODE_BACK",
+ "HOME": "KEYCODE_HOME",
+ "VOLUME_UP": "KEYCODE_VOLUME_UP",
+ "VOLUME_DOWN": "KEYCODE_VOLUME_DOWN",
+ "ENTER": "KEYCODE_ENTER",
+};
+
+const TIMEOUT = 30000;
+const MAX_BUFFER_SIZE = 1024 * 1024 * 4;
+
export class AndroidRobot implements Robot {
+ public constructor(private deviceId: string) {
+ }
+
+ public adb(...args: string[]): Buffer {
+ return execFileSync(getAdbPath(), ["-s", this.deviceId, ...args], {
+ maxBuffer: MAX_BUFFER_SIZE,
+ timeout: TIMEOUT,
+ });
+ }
+
public async getScreenSize(): Promise<Dimensions> {
- const screenSize = execSync("adb shell wm size")
+ const screenSize = this.adb("shell", "wm", "size")
.toString()
.split(" ")
.pop();
@@ -32,21 +64,8 @@ export class AndroidRobot implements Robot {
return { width, height };
}
- public adb(args: string[]): Buffer {
-
- let executable = "adb";
- if (process.env.ANDROID_HOME) {
- executable = path.join(process.env.ANDROID_HOME, "platform-tools", "adb");
- }
-
- return execFileSync(executable, args, {
- maxBuffer: 1024 * 1024 * 4,
- timeout: 30000,
- });
- }
-
public async listApps(): Promise<string[]> {
- const result = this.adb(["shell", "cmd", "package", "query-activities", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER"])
+ const result = this.adb("shell", "cmd", "package", "query-activities", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER")
.toString()
.split("\n")
.map(line => line.trim())
@@ -58,7 +77,7 @@ export class AndroidRobot implements Robot {
}
public async launchApp(packageName: string): Promise<void> {
- this.adb(["shell", "monkey", "-p", packageName, "-c", "android.intent.category.LAUNCHER", "1"]);
+ this.adb("shell", "monkey", "-p", packageName, "-c", "android.intent.category.LAUNCHER", "1");
}
public async swipe(direction: SwipeDirection): Promise<void> {
@@ -83,11 +102,11 @@ export class AndroidRobot implements Robot {
throw new Error(`Swipe direction "${direction}" is not supported`);
}
- this.adb(["shell", "input", "swipe", `${x0}`, `${y0}`, `${x1}`, `${y1}`, "1000"]);
+ this.adb("shell", "input", "swipe", `${x0}`, `${y0}`, `${x1}`, `${y1}`, "1000");
}
public async getScreenshot(): Promise<Buffer> {
- return this.adb(["shell", "screencap", "-p"]);
+ return this.adb("shell", "screencap", "-p");
}
private collectElements(node: UiAutomatorXmlNode, screenSize: Dimensions): any[] {
@@ -142,7 +161,7 @@ export class AndroidRobot implements Robot {
}
public async getElementsOnScreen(): Promise<any[]> {
- const dump = this.adb(["exec-out", "uiautomator", "dump", "/dev/tty"]);
+ const dump = this.adb("exec-out", "uiautomator", "dump", "/dev/tty");
const parser = new xml.XMLParser({
ignoreAttributes: false,
@@ -158,42 +177,34 @@ export class AndroidRobot implements Robot {
}
public async terminateApp(packageName: string): Promise<void> {
- this.adb(["shell", "am", "force-stop", packageName]);
+ this.adb("shell", "am", "force-stop", packageName);
}
public async openUrl(url: string): Promise<void> {
- this.adb(["shell", "am", "start", "-a", "android.intent.action.VIEW", "-d", url]);
+ this.adb("shell", "am", "start", "-a", "android.intent.action.VIEW", "-d", url);
}
public async sendKeys(text: string): Promise<void> {
// adb shell requires some escaping
const _text = text.replace(/ /g, "\\ ");
- this.adb(["shell", "input", "text", _text]);
+ this.adb("shell", "input", "text", _text);
}
public async pressButton(button: Button) {
- const _map = {
- "BACK": "KEYCODE_BACK",
- "HOME": "KEYCODE_HOME",
- "VOLUME_UP": "KEYCODE_VOLUME_UP",
- "VOLUME_DOWN": "KEYCODE_VOLUME_DOWN",
- "ENTER": "KEYCODE_ENTER",
- };
-
- if (!_map[button]) {
+ if (!BUTTON_MAP[button]) {
throw new Error(`Button "${button}" is not supported`);
}
- this.adb(["shell", "input", "keyevent", _map[button]]);
+ this.adb("shell", "input", "keyevent", BUTTON_MAP[button]);
}
public async tap(x: number, y: number): Promise<void> {
- this.adb(["shell", "input", "tap", `${x}`, `${y}`]);
+ this.adb("shell", "input", "tap", `${x}`, `${y}`);
}
}
export const getConnectedDevices = (): string[] => {
- return execSync(`adb devices`)
+ return execFileSync(getAdbPath(), ["devices"])
.toString()
.split("\n")
.filter(line => !line.startsWith("List of devices attached"))
diff --git a/src/ios.ts b/src/ios.ts
new file mode 100644
index 0000000..88ff85e
--- /dev/null
+++ b/src/ios.ts
@@ -0,0 +1,85 @@
+
+import { readFileSync, unlinkSync } from "fs";
+import { execFileSync } from "child_process";
+
+import { WebDriverAgent } from "./webdriver-agent";
+import { Button, Dimensions, Robot, SwipeDirection } from "./robot";
+
+interface ListCommandOutput {
+ deviceList: string[];
+}
+
+const getGoIosPath = (): string => {
+ return "ios";
+};
+
+export class IosRobot implements Robot {
+
+ private readonly wda: WebDriverAgent;
+
+ public constructor(private deviceId: string) {
+ this.wda = new WebDriverAgent("localhost", 8100);
+ }
+
+ private async ios(...args: string[]): Promise<string> {
+ return execFileSync(getGoIosPath(), ["--udid", this.deviceId, ...args], {}).toString();
+ }
+
+ public async getScreenSize(): Promise<Dimensions> {
+ return await this.wda.getScreenSize();
+ }
+
+ public async swipe(direction: SwipeDirection): Promise<void> {
+ await this.wda.swipe(direction);
+ }
+
+ public async listApps(): Promise<string[]> {
+ const output = await this.ios("apps", "--all", "--list");
+ return output
+ .split("\n")
+ .map(line => line.split(" ")[0]);
+ }
+
+ public async launchApp(packageName: string): Promise<void> {
+ await this.ios("launch", packageName);
+ }
+
+ public async terminateApp(packageName: string): Promise<void> {
+ await this.ios("kill", packageName);
+ }
+
+ public async openUrl(url: string): Promise<void> {
+ await this.wda.openUrl(url);
+ }
+
+ public async sendKeys(text: string): Promise<void> {
+ await this.wda.sendKeys(text);
+ }
+
+ public async pressButton(button: Button): Promise<void> {
+ await this.wda.pressButton(button);
+ }
+
+ public async tap(x: number, y: number): Promise<void> {
+ await this.wda.tap(x, y);
+ }
+
+ public async getElementsOnScreen(): Promise<any[]> {
+ return await this.wda.getElementsOnScreen();
+ }
+
+ public async getScreenshot(): Promise<Buffer> {
+ await this.ios("screenshot", "--output", "screenshot.png");
+ const buffer = readFileSync("screenshot.png");
+ unlinkSync("screenshot.png");
+ return buffer;
+ }
+}
+
+export class IosManager {
+ public async listDevices(): Promise<string[]> {
+ const output = execFileSync(getGoIosPath(), ["list"]).toString();
+ const json: ListCommandOutput = JSON.parse(output);
+ return json.deviceList;
+ }
+}
diff --git a/src/iphone-simulator.ts b/src/iphone-simulator.ts
index 6f64738..99a1b27 100644
--- a/src/iphone-simulator.ts
+++ b/src/iphone-simulator.ts
@@ -1,4 +1,6 @@
import { execFileSync, execSync } from "child_process";
+
+import { WebDriverAgent } from "./webdriver-agent";
import { Button, Dimensions, Robot, SwipeDirection } from "./robot";
export interface Simulator {
@@ -7,26 +9,6 @@ export interface Simulator {
state: string;
}
-interface SourceTreeElement {
- type: string;
- label?: string;
- name?: string;
- rawIdentifier?: string;
- rect: {
- x: number;
- y: number;
- width: number;
- height: number;
- };
-
- children?: Array<SourceTreeElement>;
-}
-
-interface SourceTree {
- value: SourceTreeElement;
-}
-
-
interface AppInfo {
ApplicationType: string;
Bundle: string;
@@ -41,16 +23,26 @@ interface AppInfo {
SBAppTags: string[];
}
+const TIMEOUT = 30000;
+const MAX_BUFFER_SIZE = 1024 * 1024 * 4;
export class Simctl implements Robot {
+
+ private readonly wda: WebDriverAgent;
+
constructor(private readonly simulatorUuid: string) {
+ this.wda = new WebDriverAgent("localhost", 8100);
}
private simctl(...args: string[]): Buffer {
return execFileSync(
"xcrun",
["simctl", ...args],
- { maxBuffer: 1024 * 1024 * 4 });
+ {
+ timeout: TIMEOUT,
+ maxBuffer: MAX_BUFFER_SIZE,
+ }
+ );
}
public async getScreenshot(): Promise<Buffer> {
@@ -58,7 +50,8 @@ export class Simctl implements Robot {
}
public async openUrl(url: string) {
- this.simctl("openurl", this.simulatorUuid, url);
+ await this.wda.openUrl(url);
+ // alternative: this.simctl("openurl", this.simulatorUuid, url);
}
public async launchApp(packageName: string) {
@@ -147,197 +140,27 @@ export class Simctl implements Robot {
}
public async getScreenSize(): Promise<Dimensions> {
- return this.withinSession(async (port, sessionId) => {
- const url = `http://localhost:${port}/session/${sessionId}/window/size`;
- const response = await fetch(url);
- const json = await response.json();
- return {
- width: json.value.width,
- height: json.value.height
- };
- });
+ return this.wda.getScreenSize();
}
public async sendKeys(keys: string) {
- await this.withinSession(async (port, sessionId) => {
- const url = `http://localhost:${port}/session/${sessionId}/wda/keys`;
- await fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ value: [keys] }),
- });
- });
+ return this.wda.sendKeys(keys);
}
public async swipe(direction: SwipeDirection) {
- await this.withinSession(async (port, sessionId) => {
-
- const x0 = 200;
- let y0 = 600;
- const x1 = 200;
- let y1 = 200;
-
- if (direction === "up") {
- const tmp = y0;
- y0 = y1;
- y1 = tmp;
- }
-
- const url = `http://localhost:${port}/session/${sessionId}/actions`;
- await fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- actions: [
- {
- type: "pointer",
- id: "finger1",
- parameters: { pointerType: "touch" },
- actions: [
- { type: "pointerMove", duration: 0, x: x0, y: y0 },
- { type: "pointerDown", button: 0 },
- { type: "pointerMove", duration: 0, x: x1, y: y1 },
- { type: "pause", duration: 1000 },
- { type: "pointerUp", button: 0 }
- ]
- }
- ]
- }),
- });
- });
+ return this.wda.swipe(direction);
}
public async tap(x: number, y: number) {
- await this.withinSession(async (port, sessionId) => {
- const url = `http://localhost:${port}/session/${sessionId}/actions`;
- await fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- actions: [
- {
- type: "pointer",
- id: "finger1",
- parameters: { pointerType: "touch" },
- actions: [
- { type: "pointerMove", duration: 0, x, y },
- { type: "pointerDown", button: 0 },
- { type: "pause", duration: 100 },
- { type: "pointerUp", button: 0 }
- ]
- }
- ]
- }),
- });
- });
+ return this.wda.tap(x, y);
}
public async pressButton(button: Button) {
- const _map = {
- "HOME": "home",
- "VOLUME_UP": "volumeup",
- "VOLUME_DOWN": "volumedown",
- };
-
- if (button === "ENTER") {
- await this.sendKeys("\n");
- return;
- }
-
- // Type assertion to check if button is a key of _map
- if (!(button in _map)) {
- throw new Error(`Button "${button}" is not supported`);
- }
-
- await this.withinSession(async (port, sessionId) => {
- const url = `http://localhost:${port}/session/${sessionId}/wda/pressButton`;
- const response = await fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- name: button,
- }),
- });
-
- return response.json();
- });
- }
-
- private async createSession(port: number) {
- const url = `http://localhost:${port}/session`;
- const response = await fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ capabilities: { alwaysMatch: { platformName: "iOS" } } }),
- });
-
- const json = await response.json();
- return json.value.sessionId;
- }
-
- private async deleteSession(port: number, sessionId: string) {
- const url = `http://localhost:${port}/session/${sessionId}`;
- const response = await fetch(url, { method: "DELETE" });
- return response.json();
- }
-
- private async withinSession(fn: (port: number, sessionId: string) => Promise<any>) {
- const port = 8100;
- const sessionId = await this.createSession(port);
- const result = await fn(port, sessionId);
- await this.deleteSession(port, sessionId);
- return result;
- }
-
- private filterSourceElements(source: SourceTreeElement): Array<any> {
-
- const output: any[] = [];
-
- console.error("gilm " + JSON.stringify(source));
- if (["TextField", "Button", "Switch"].includes(source.type)) {
- output.push({
- type: source.type,
- label: source.label,
- name: source.name,
- rect: {
- x0: source.rect.x,
- y0: source.rect.y,
- x1: source.rect.x + source.rect.width,
- y1: source.rect.y + source.rect.height,
- },
- });
- }
-
- if (source.children) {
- for (const child of source.children) {
- output.push(...this.filterSourceElements(child));
- }
- }
-
- return output;
- }
-
- public async getPageSource(): Promise<SourceTree> {
- const port = 8100;
- const url = `http://localhost:${port}/source/?format=json`;
- const response = await fetch(url);
- const json = await response.json();
- return json as SourceTree;
+ await this.wda.pressButton(button);
}
public async getElementsOnScreen(): Promise<any[]> {
- const source = await this.getPageSource();
- return this.filterSourceElements(source.value);
+ return await this.wda.getElementsOnScreen();
}
}
diff --git a/src/server.ts b/src/server.ts
index 5983053..03b5228 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -7,6 +7,7 @@ import { error, trace } from "./logger";
import { AndroidRobot, getConnectedDevices } from "./android";
import { Robot } from "./robot";
import { SimctlManager } from "./iphone-simulator";
+import { IosManager, IosRobot } from "./ios";
const getAgentVersion = (): string => {
const json = require("../package.json");
@@ -49,30 +50,38 @@ export const createMcpServer = (): McpServer => {
const simulatorManager = new SimctlManager();
tool(
- "list_available_devices",
+ "mobile_list_available_devices",
"List all available devices. This includes both physical devices and simulators. If there is more than one device returned, you need to let the user select one of them.",
{},
async ({}) => {
+ const iosManager = new IosManager();
const devices = await simulatorManager.listBootedSimulators();
const simulatorNames = devices.map(d => d.name);
const androidDevices = getConnectedDevices();
- return `Found these iOS simulators: [${simulatorNames.join(".")}] and Android devices: [${androidDevices.join(",")}]`;
+ const iosDevices = await iosManager.listDevices();
+ return `Found these iOS simulators: [${simulatorNames.join(".")}], iOS devices: [${iosDevices.join(",")}] and Android devices: [${androidDevices.join(",")}]`;
}
);
tool(
- "use_device",
+ "mobile_use_device",
"Select a device to use. This can be a simulator or an Android device. Use the list_available_devices tool to get a list of available devices.",
{
device: z.string().describe("The name of the device to select"),
- deviceType: z.enum(["simulator", "android"]).describe("The type of device to select"),
+ deviceType: z.enum(["simulator", "ios", "android"]).describe("The type of device to select"),
},
async ({ device, deviceType }) => {
console.log(device, deviceType);
- if (deviceType === "simulator") {
- robot = simulatorManager.getSimulator(device);
- } else {
- robot = new AndroidRobot(); // TODO: device);
+ switch (deviceType) {
+ case "simulator":
+ robot = simulatorManager.getSimulator(device);
+ break;
+ case "ios":
+ robot = new IosRobot(device);
+ break;
+ case "android":
+ robot = new AndroidRobot(device);
+ break;
}
return `Selected device: ${device} (${deviceType})`;
diff --git a/src/webdriver-agent.ts b/src/webdriver-agent.ts
new file mode 100644
index 0000000..a227e39
--- /dev/null
+++ b/src/webdriver-agent.ts
@@ -0,0 +1,227 @@
+import { SwipeDirection } from "./robot";
+
+interface SourceTreeElement {
+ type: string;
+ label?: string;
+ name?: string;
+ rawIdentifier?: string;
+ rect: {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ };
+
+ children?: Array<SourceTreeElement>;
+}
+
+interface SourceTree {
+ value: SourceTreeElement;
+}
+
+export class WebDriverAgent {
+
+ constructor(private readonly host: string, private readonly port: number) {
+ }
+
+ public async createSession() {
+ const url = `http://${this.host}:${this.port}/session`;
+ const response = await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ capabilities: { alwaysMatch: { platformName: "iOS" } } }),
+ });
+
+ const json = await response.json();
+ return json.value.sessionId;
+ }
+
+ public async deleteSession(sessionId: string) {
+ const url = `http://${this.host}:${this.port}/session/${sessionId}`;
+ const response = await fetch(url, { method: "DELETE" });
+ return response.json();
+ }
+
+ public async withinSession(fn: (url: string) => Promise<any>) {
+ const sessionId = await this.createSession();
+ const url = `http://${this.host}:${this.port}/session/${sessionId}`;
+ const result = await fn(url);
+ await this.deleteSession(sessionId);
+ return result;
+ }
+
+ public async getScreenSize() {
+ return this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/wda/screen`;
+ const response = await fetch(url);
+ const json = await response.json();
+ return {
+ width: json.value.screenSize.width * json.value.scale,
+ height: json.value.screenSize.height * json.value.scale,
+ };
+ });
+ }
+
+ public async sendKeys(keys: string) {
+ await this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/wda/keys`;
+ await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ value: [keys] }),
+ });
+ });
+ }
+
+ public async pressButton(button: string) {
+ const _map = {
+ "HOME": "home",
+ "VOLUME_UP": "volumeup",
+ "VOLUME_DOWN": "volumedown",
+ };
+
+ if (button === "ENTER") {
+ await this.sendKeys("\n");
+ return;
+ }
+
+ // Type assertion to check if button is a key of _map
+ if (!(button in _map)) {
+ throw new Error(`Button "${button}" is not supported`);
+ }
+
+ await this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/wda/pressButton`;
+ const response = await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ name: button,
+ }),
+ });
+
+ return response.json();
+ });
+ }
+
+ public async tap(x: number, y: number) {
+ await this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/actions`;
+ await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ actions: [
+ {
+ type: "pointer",
+ id: "finger1",
+ parameters: { pointerType: "touch" },
+ actions: [
+ { type: "pointerMove", duration: 0, x, y },
+ { type: "pointerDown", button: 0 },
+ { type: "pause", duration: 100 },
+ { type: "pointerUp", button: 0 }
+ ]
+ }
+ ]
+ }),
+ });
+ });
+ }
+
+ private filterSourceElements(source: SourceTreeElement): Array<any> {
+
+ const output: any[] = [];
+
+ if (["TextField", "Button", "Switch"].includes(source.type)) {
+ output.push({
+ type: source.type,
+ label: source.label,
+ name: source.name,
+ rect: {
+ x0: source.rect.x,
+ y0: source.rect.y,
+ x1: source.rect.x + source.rect.width,
+ y1: source.rect.y + source.rect.height,
+ },
+ });
+ }
+
+ if (source.children) {
+ for (const child of source.children) {
+ output.push(...this.filterSourceElements(child));
+ }
+ }
+
+ return output;
+ }
+
+ public async getPageSource(): Promise<SourceTree> {
+ const url = `http://${this.host}:${this.port}/source/?format=json`;
+ const response = await fetch(url);
+ const json = await response.json();
+ return json as SourceTree;
+ }
+
+ public async getElementsOnScreen(): Promise<any[]> {
+ const source = await this.getPageSource();
+ return this.filterSourceElements(source.value);
+ }
+
+ public async openUrl(url: string): Promise<void> {
+ await this.withinSession(async sessionUrl => {
+ await fetch(`${sessionUrl}/url`, {
+ method: "POST",
+ body: JSON.stringify({ url }),
+ });
+ });
+ }
+
+ public async swipe(direction: SwipeDirection) {
+ await this.withinSession(async sessionUrl => {
+
+ const x0 = 200;
+ let y0 = 600;
+ const x1 = 200;
+ let y1 = 200;
+
+ if (direction === "up") {
+ const tmp = y0;
+ y0 = y1;
+ y1 = tmp;
+ }
+
+ const url = `${sessionUrl}/actions`;
+ await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ actions: [
+ {
+ type: "pointer",
+ id: "finger1",
+ parameters: { pointerType: "touch" },
+ actions: [
+ { type: "pointerMove", duration: 0, x: x0, y: y0 },
+ { type: "pointerDown", button: 0 },
+ { type: "pointerMove", duration: 0, x: x1, y: y1 },
+ { type: "pause", duration: 1000 },
+ { type: "pointerUp", button: 0 }
+ ]
+ }
+ ]
+ }),
+ });
+ });
+ }
+}
diff --git a/test/android.ts b/test/android.ts
index e0c5ec4..192ed43 100644
--- a/test/android.ts
+++ b/test/android.ts
@@ -1,10 +1,17 @@
-
import assert from "assert";
-import { AndroidRobot } from "../src/android";
+
import sharp from "sharp";
+import { AndroidRobot, getConnectedDevices } from "../src/android";
+
describe("android", () => {
- const android = new AndroidRobot();
+
+ const devices = getConnectedDevices();
+ if (devices.length === 0) {
+ throw new Error("No Android devices found");
+ }
+
+ const android = new AndroidRobot(devices[0]);
it("should be able to get the screen size", async () => {
const screenSize = await android.getScreenSize();
@@ -32,12 +39,12 @@ describe("android", () => {
});
it("should be able to open a url", async () => {
- await android.adb(["shell", "input", "keyevent", "KEYCODE_HOME"]);
+ await android.adb("shell", "input", "keyevent", "KEYCODE_HOME");
await android.openUrl("https://www.example.com");
});
it("should be able to list elements on screen", async () => {
- await android.adb(["shell", "input", "keyevent", "KEYCODE_HOME"]);
+ await android.adb("shell", "input", "keyevent", "KEYCODE_HOME");
await android.openUrl("https://www.example.com");
const elements = await android.getElementsOnScreen();
diff --git a/test/iphone-simulator.ts b/test/iphone-simulator.ts
index 6a1ea4e..696f06b 100644
--- a/test/iphone-simulator.ts
+++ b/test/iphone-simulator.ts
@@ -1,4 +1,5 @@
import assert from "assert";
+
import { SimctlManager } from "../src/iphone-simulator";
describe("ios", () => {