Node.js & Appium 2.0:通过NPM一键安装最新版服务器:
npm install -g appium
appium driver install uiautomator2 # 安装安卓驱动
Python依赖:使用appium-python-client库:
pip install appium-python-client selenium
真机调试:开启开发者选项→USB调试→通过adb devices验证连接
模拟器推荐:网易MuMu/Genymotion,性能优化优于原生模拟器
from appium import webdriver
from appium.options.android import UiAutomator2Options
capabilities = {
"platformName": "Android",
"automationName": "uiautomator2",
"deviceName": "Your_Device_ID", # adb devices获取
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI",
"noReset": True # 保留登录态
}
driver = webdriver.Remote('http://localhost:4723', options=UiAutomator2Options().load_capabilities(capabilities))
# 点击微信输入框
search = driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text("搜索")')
search.click()
# 输入联系人并发送消息
driver.find_element(AppiumBy.ID, "com.tencent.mm:id/search_input").send_keys("测试账号")
driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")[0].click()
driver.find_element(AppiumBy.ID, "com.tencent.mm:id/chatting_content_et").send_keys("Appium自动化测试成功!")
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "发送").click()
driver.quit()
# 滑动到页面底部
window_size = driver.get_window_size()
driver.swipe(
start_x=window_size['width']*0.5,
start_y=window_size['height']*0.8,
end_x=window_size['width']*0.5,
end_y=window_size['height']*0.2,
duration=1000
)
# 抓取列表数据
items = driver.find_elements(AppiumBy.XPATH, '//android.widget.ListView/android.widget.TextView')
for item in items:
print(f"抓取内容: {item.text}")
优先使用resource-id(ID定位),避免XPath性能损耗
使用UIAutomatorViewer或Appium Inspector分析页面结构
driver.implicitly_wait(15) # 全局隐式等待
from selenium.common.exceptions import NoSuchElementException
try:
el = driver.find_element(AppiumBy.ID, "com.example:id/button")
except NoSuchElementException:
print("元素加载超时,启动重试机制")
推荐Sonic云真机平台,实现多设备并行测试
阿里云开发者社区提供Appium Desktop镜像快速部署方案
跨平台支持:通过appium-chromium-driver实现Chrome/Edge浏览器自动化
IoT设备控制:实验性支持智能电视和车载系统自动化
持续集成:结合Jenkins+Appium实现每日自动化回归测试