Appium 是一个开源的移动应用自动化测试框架,支持跨平台(iOS、Android)的原生应用、混合应用和移动网页的自动化测试。它基于 WebDriver 协议,因此很多操作命令和 Selenium 类似。以下是 Appium 常用的一些命令,主要分为查找元素、元素操作、设备操作等几类。
1. 元素查找命令
这些命令用于查找页面上的元素,类似于 Selenium 中的元素查找方式。
-
find_element_by_id
按照元素的 ID 查找单个元素:element = driver.find_element_by_id("element_id")
-
find_elements_by_id
查找多个具有相同 ID 的元素:elements = driver.find_elements_by_id("element_id")
-
find_element_by_xpath
使用 XPath 表达式查找元素:element = driver.find_element_by_xpath("//android.widget.TextView[@text='Login']")
-
find_element_by_accessibility_id
查找具有特定无障碍 ID 的元素:element = driver.find_element_by_accessibility_id("element_accessibility_id")
-
find_element_by_class_name
按类名查找元素:element = driver.find_element_by_class_name("android.widget.Button")
-
find_element_by_android_uiautomator
(仅 Android)
使用 UIAutomator 表达式查找元素:element = driver.find_element_by_android_uiautomator('new UiSelector().text("Login")')
2. 元素操作命令
这些命令用于对找到的元素进行操作,比如点击、输入文本等。
-
click
点击元素:element.click()
-
send_keys
向输入框或文本框输入文字:element.send_keys("Hello Appium")
-
clear
清除输入框中的内容:element.clear()
-
is_displayed
检查元素是否显示在页面上:element.is_displayed()
-
get_attribute
获取元素的属性值:value = element.get_attribute("content-desc")
3. 设备操作命令
这些命令用于控制设备的操作,比如锁屏、截屏、后台运行等。
-
lock
锁定设备:driver.lock()
-
unlock
解锁设备:driver.unlock()
-
is_locked
检查设备是否处于锁定状态:driver.is_locked()
-
shake
(仅 iOS)
模拟设备摇动:driver.shake()
-
open_notifications
(仅 Android)
打开 Android 的通知栏:driver.open_notifications()
-
toggle_wifi
切换设备的 Wi-Fi 状态:driver.toggle_wifi()
-
toggle_airplane_mode
切换设备的飞行模式:driver.toggle_airplane_mode()
-
pull_file
从设备中拉取文件:driver.pull_file("/path/to/file")
-
push_file
向设备中推送文件:driver.push_file("/path/on/device", "/path/on/local")
-
get_screenshot_as_file
截取当前屏幕并保存到文件:driver.get_screenshot_as_file("screenshot.png")
4. 其他常用命令
这些命令用于对应用、会话进行控制或获取设备相关信息。
-
start_activity
(仅 Android)
启动指定的应用活动(Activity):driver.start_activity("com.package.name", "com.package.name.ActivityName")
-
background_app
将应用置于后台一段时间:driver.background_app(5) # 5秒后恢复前台
-
reset
重置应用,将应用数据清空:driver.reset()
-
install_app
安装应用:driver.install_app("/path/to/app.apk")
-
remove_app
卸载应用:driver.remove_app("com.package.name")
-
is_app_installed
检查应用是否已安装:driver.is_app_installed("com.package.name")
-
terminate_app
终止应用进程:driver.terminate_app("com.package.name")
-
launch_app
启动应用:driver.launch_app()
-
close_app
关闭当前应用:driver.close_app()
-
get_device_time
获取设备的当前时间:device_time = driver.get_device_time()
-
get_page_source
获取当前页面的 XML 源码:page_source = driver.get_page_source()
5. 手势操作命令
Appium 支持复杂的手势操作,比如滑动、长按、拖拽等。
-
swipe
(已过时)
滑动屏幕,现多用touch_action
模拟:driver.swipe(start_x, start_y, end_x, end_y, duration=1000)
-
tap
单击屏幕上的某个位置:from appium.webdriver.common.touch_action import TouchAction action = TouchAction(driver) action.tap(x=100, y=200).perform()
-
long_press
长按屏幕上的某个位置或元素:action.long_press(x=100, y=200, duration=3000).release().perform()
-
scroll
滚动屏幕(从一个元素滚动到另一个元素):driver.scroll(element1, element2)
-
pinch
双指缩放(捏合)操作:driver.pinch(element)
-
zoom
双指放大操作:driver.zoom(element)
在 Appium 中,使用 AppiumBy
是一种更现代和清晰的方式来定位元素。
要使用 AppiumBy
,你可以从 appium.webdriver.common.appiumby
导入 AppiumBy
,并利用它提供的各种定位方法。
常用的 AppiumBy
方法
AppiumBy
提供了比传统 By
类更多的定位方式,特别适用于移动平台:
-
AppiumBy.ID
根据元素的 ID 查找元素。element = driver.find_element(AppiumBy.ID, "element_id")
-
AppiumBy.XPATH
使用 XPath 表达式查找元素。element = driver.find_element(AppiumBy.XPATH, "//android.widget.TextView[@text='Login']")
-
AppiumBy.ACCESSIBILITY_ID
使用无障碍 ID(Accessibility ID)查找元素。这个方法适用于跨平台查找元素。element = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "element_accessibility_id")
-
AppiumBy.CLASS_NAME
按类名查找元素。element = driver.find_element(AppiumBy.CLASS_NAME, "android.widget.Button")
-
AppiumBy.ANDROID_UIAUTOMATOR
使用 Android 的 UIAutomator 表达式查找元素。仅适用于 Android 平台。element = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Login")')
-
AppiumBy.IOS_CLASS_CHAIN
使用 iOS Class Chain 表达式查找元素。仅适用于 iOS 平台。element = driver.find_element(AppiumBy.IOS_CLASS_CHAIN, "**/XCUIElementTypeButton[`name == 'Login'`]")
-
AppiumBy.IOS_PREDICATE_STRING
使用 iOS NSPredicate 字符串查找元素。仅适用于 iOS 平台。element = driver.find_element(AppiumBy.IOS_PREDICATE_STRING, "type == 'XCUIElementTypeButton' AND name == 'Login'")
-
AppiumBy.TAG_NAME
按标签名称查找元素。element = driver.find_element(AppiumBy.TAG_NAME, "input")
-
AppiumBy.NAME
按元素的name
属性查找元素。element = driver.find_element(AppiumBy.NAME, "element_name")
示例代码
假设你要查找一个具有 ID login_button
的按钮并点击它,代码如下:
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
# 初始化 Appium WebDriver
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities)
# 使用 AppiumBy 查找元素并点击
login_button = driver.find_element(AppiumBy.ID, "login_button")
login_button.click()
AppiumBy
的优势
- 更具可读性和一致性:
AppiumBy
提供了专门为移动平台设计的定位方法,能够更直观地表达定位逻辑。 - 跨平台支持:
AppiumBy.ACCESSIBILITY_ID
可以在 Android 和 iOS 平台之间无缝切换,简化跨平台测试用例的编写。 - 支持高级查找方式:比如
AppiumBy.IOS_CLASS_CHAIN
和AppiumBy.ANDROID_UIAUTOMATOR
支持平台特有的高级定位方式,适应复杂场景。
通过使用 AppiumBy
,你可以更方便地编写 Appium 自动化测试脚本,提升代码的可维护性和可读性。