mcp-appium-visual 是一个集成模型上下文协议(MCP)的人工智能驱动移动自动化平台。它通过 Appium 实现对 Android 和 iOS 设备的无缝控制,具备智能视觉元素检测和恢复功能。
在执行任何命令之前,请确保您的环境变量已正确设置:
.bash_profile、.zshrc 或其他 shell 配置文件中包含必要的环境变量:# 示例环境变量在 ~/.bash_profile 中
export JAVA_HOME=/path/to/your/java
export ANDROID_HOME=/path/to/your/android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
source ~/.bash_profile # 对于 bash
# 或者
source ~/.zshrc # 对于 zsh
注意:系统会在初始化驱动程序时自动尝试加载您的
.bash_profile,但在新的终端会话中运行测试之前手动确保环境设置正确是推荐的做法。
对于 iOS 测试,正确的 Xcode 命令行工具配置至关重要:
xcode-select --install
xcode-select -p
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -license accept
对于 iOS 真实设备测试,确保您的 Apple Developer 账户已在 Xcode 中正确配置:
设置 iOS 开发环境变量:
# 将这些添加到您的 ~/.bash_profile 或 ~/.zshrc 中
export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"
export PATH="$DEVELOPER_DIR/usr/bin:$PATH"
source ~/.bash_profile # 对于 bash
# 或者
source ~/.zshrc # 对于 zsh
npm install
npm install -g appium
appium
设置 Android 设备/模拟器:
adb devices 验证设备是否连接对于 iOS 测试(仅限 macOS):
xcode-select --installnpm run build
npm run dev
npm test
示例测试使用 Android 设置应用作为演示。要测试您自己的应用:
编辑 examples/appium-test.ts:
deviceName 以匹配您的设备app 路径指向您的 APK 文件,或者appPackage 和 appActivity 以测试已安装的应用常见能力配置:
const capabilities: AppiumCapabilities = {
platformName: "Android",
deviceName: "YOUR_DEVICE_NAME",
automationName: "UiAutomator2",
// 对于安装和测试 APK:
app: "./path/to/your/app.apk",
// 或者对于测试已安装的应用:
appPackage: "your.app.package",
appActivity: ".MainActivity",
noReset: true,
};
对于使用新 Xcode 命令行支持的 iOS 测试:
examples/xcode-appium-example.ts 中:const capabilities: AppiumCapabilities = {
platformName: "iOS",
deviceName: "iPhone 13", // 您的模拟器或设备名称
automationName: "XCUITest",
udid: "DEVICE_UDID", // 从 XcodeCommands.getIosSimulators() 获取
// 对于安装和测试应用:
app: "./path/to/your/app.app",
// 或者对于测试已安装的应用:
bundleId: "com.your.app",
noReset: true,
};
MCP 服务器支持各种 Appium 操作:
元素交互:
应用管理:
设备控制:
高级功能:
Xcode 命令行工具(仅限 iOS):
MCP-Appium 库现在实现了 W3C WebDriver Actions API 用于触摸手势,这是现代移动自动化的标准。
tapElement 方法现在使用 W3C Actions API,并带有智能回退:
// 方法将按以下顺序尝试:
// 1. 标准 WebdriverIO 点击()
// 2. W3C Actions API
// 3. 遗留 TouchAction API(为了向后兼容性)
await appium.tapElement("//android.widget.Button[@text='OK']");
// 或者使用点击别名
await appium.click("//android.widget.Button[@text='OK']");
scrollToElement 方法现在使用 W3C Actions API:
// 使用 W3C Actions API 进行更可靠的滚动
await appium.scrollToElement(
"//android.widget.TextView[@text='关于手机']", // 选择器
"down", // 方向:"up"、"down"、"left"、"right"
"xpath", // 策略
10 // 最大滚动次数
);
您可以使用 executeMobileCommand 方法创建自己的自定义 W3C 手势:
// 创建自定义 W3C Actions API 手势
const w3cActions = {
actions: [
{
type: "pointer",
id: "finger1",
parameters: { pointerType: "touch" },
actions: [
// 移动到起始位置
{ type: "pointerMove", duration: 0, x: startX, y: startY },
// 按下
{ type: "pointerDown", button: 1 },
// 在持续时间毫秒内移动到结束位置
{
type: "pointerMove",
duration: duration,
origin: "viewport",
x: endX,
y: endY,
},
// 释放
{ type: "pointerUp", button: 1 },
],
},
],
};
// 使用 executeScript 执行 W3C Actions
await appium.executeMobileCommand("performActions", [w3cActions.actions]);
查看 examples/w3c-actions-swipe-demo.ts 以获取更多 W3C 标准手势实现示例。
新的 XcodeCommands 类提供了强大的 iOS 测试工具:
import { XcodeCommands } from "../src/lib/xcode/xcodeCommands.js";
// 检查 Xcode CLI 工具是否已安装
const isInstalled = await XcodeCommands.isXcodeCliInstalled();
// 获取可用模拟器
const simulators = await XcodeCommands.getIosSimulators();
// 启动模拟器
await XcodeCommands.bootSimulator("SIMULATOR_UDID");
// 安装应用
await XcodeCommands.installApp("SIMULATOR_UDID", "/path/to/app.app");
// 启动应用
await XcodeCommands.launchApp("SIMULATOR_UDID", "com.example.app");
// 截图
await XcodeCommands.takeScreenshot("SIMULATOR_UDID", "/path/to/output.png");
// 关闭模拟器
await XcodeCommands.shutdownSimulator("SIMULATOR_UDID");
click() 方法提供了一个比 tapElement() 更直观的替代方案:
// 使用点击方法
await appium.click("//android.widget.Button[@text='OK']");
// 这相当于:
await appium.tapElement("//android.widget.Button[@text='OK']");
设备未找到:
adb devices 输出应用未安装:
元素未找到:
连接问题:
iOS 模拟器问题:
xcode-select -pxcrun simctl list devices 检查模拟器 UDID 是否正确欢迎提交问题和拉取请求以增加新功能或修复错误。
MIT