Mobile Appium Test
Android UI automation testing using Appium with USB-connected real devices.
Prerequisites
Required tools (must be installed):
- - ADB (Android Debug Bridge) - part of Android SDK
- Appium Server (v2.x recommended)
- Appium Doctor (
npm install -g @appium/doctor)
Verify installation:
CODEBLOCK0
Quick Reference
Device Connection
| Goal | Command |
|---|
| List connected devices | INLINECODE1 |
| Get device info |
adb shell getprop ro.build.version.release |
| Restart ADB server |
adb kill-server && adb start-server |
| USB debug authorization | Check phone for authorization prompt |
Appium Server
| Goal | Command |
|---|
| Start Appium | INLINECODE4 |
| Start with relaxed security |
appium --relaxed-security |
| Check Appium status |
curl http://127.0.0.1:4723/status |
Common Appium Operations
| Goal | Endpoint/Action |
|---|
| Start session | INLINECODE7 with capabilities |
| Find element |
POST /session/{id}/element |
| Click element |
POST /session/{id}/element/{id}/click |
| Send keys |
POST /session/{id}/element/{id}/value |
| Take screenshot |
GET /session/{id}/screenshot |
| Get page source |
GET /session/{id}/source |
| Quit session |
DELETE /session/{id} |
Typical Workflow
1. Verify Device Connection
adb devices
Ensure device shows
device status (not
unauthorized or
offline).
2. Start Appium Server
CODEBLOCK2
3. Run Test
Use desired capabilities for USB device:
CODEBLOCK3
4. Common Test Scenarios
- - Install app: INLINECODE17
- Launch app: Appium
appActivity capability - Find element by ID: INLINECODE19
- Find element by text: INLINECODE20
- Swipe: Appium touch action
- Get logs: INLINECODE21
Error Handling
| Error | Cause | Solution |
|---|
| INLINECODE22 | USB connection issue | Check adb devices, restart ADB server |
| INLINECODE24 |
USB debug not authorized | Unlock phone, authorize the computer |
|
no such element | Element not found | Use
find_elements with wait, check page source |
|
session not created | Capability mismatch | Verify UDID, platform version, app path |
Notes
- - Always use
UdID from adb devices for real device testing - Use
UiAutomator2 as automation engine for Android - INLINECODE31 preserves app state between sessions
- For WiFi debugging:
adb tcpip 5555 then INLINECODE33
移动端Appium测试
使用Appium通过USB连接真实设备进行Android UI自动化测试。
前置条件
必需工具(必须安装):
- - ADB(Android调试桥)- Android SDK的一部分
- Appium服务器(推荐v2.x版本)
- Appium Doctor(npm install -g @appium/doctor)
验证安装:
bash
adb version
appium --version
appium doctor
快速参考
设备连接
| 目标 | 命令 |
|---|
| 列出已连接设备 | adb devices |
| 获取设备信息 |
adb shell getprop ro.build.version.release |
| 重启ADB服务器 | adb kill-server && adb start-server |
| USB调试授权 | 检查手机上的授权提示 |
Appium服务器
| 目标 | 命令 |
|---|
| 启动Appium | appium --address 127.0.0.1 --port 4723 |
| 以宽松安全模式启动 |
appium --relaxed-security |
| 检查Appium状态 | curl http://127.0.0.1:4723/status |
常见Appium操作
| 目标 | 端点/操作 |
|---|
| 启动会话 | POST /session 附带能力参数 |
| 查找元素 |
POST /session/{id}/element |
| 点击元素 | POST /session/{id}/element/{id}/click |
| 输入文本 | POST /session/{id}/element/{id}/value |
| 截取屏幕截图 | GET /session/{id}/screenshot |
| 获取页面源码 | GET /session/{id}/source |
| 退出会话 | DELETE /session/{id} |
典型工作流程
1. 验证设备连接
bash
adb devices
确保设备显示device状态(而非unauthorized或offline)。
2. 启动Appium服务器
bash
appium --address 127.0.0.1 --port 4723 --relaxed-security
3. 运行测试
使用USB设备的期望能力参数:
json
{
platformName: Android,
deviceName: device,
udid: <设备UDID>,
app: /path/to/app.apk,
automationName: UiAutomator2,
noReset: true
}
4. 常见测试场景
- - 安装应用:adb install app.apk
- 启动应用:Appium的appActivity能力参数
- 按ID查找元素:findelement(id, com.example:id/button)
- 按文本查找元素:findelement(xpath, //*[@text=Submit])
- 滑动操作:Appium触摸操作
- 获取日志:adb logcat
错误处理
| 错误 | 原因 | 解决方案 |
|---|
| device not found | USB连接问题 | 检查adb devices,重启ADB服务器 |
| unauthorized |
USB调试未授权 | 解锁手机,授权计算机 |
| no such element | 元素未找到 | 使用带等待的find_elements,检查页面源码 |
| session not created | 能力参数不匹配 | 验证UDID、平台版本、应用路径 |
注意事项
- - 对于真实设备测试,始终使用adb devices中的UDID
- 使用UiAutomator2作为Android的自动化引擎
- noReset: true可在会话之间保留应用状态
- WiFi调试:adb tcpip 5555 然后 adb connect :5555