Android测试
工具
Appium、adb命令、uiautomation、pymouse、pykeyboard
adb常用命令
-
Monkey测试:触发随机事件流的自动化测试;场景:冒烟、稳定性测试;关注:ANR、Crash、exception比例
adb shell monkey 100
常用参数:
-v 日志详细度(v→vvv)-s seed值,指定即复现,没有会随机生成
-p 指定包名,不指定则随机选择
–throttle 两事件间的延迟时间,降低cpu压力;一般取300ms
–ignore-crashes 忽略异常崩溃
–ignore-timeouts 忽略ANR
–pct-touch 设置触摸事件的百分比
如:adb -s 127.0.0.1:62001 shell monkey -p com.***m 1000 -vv -s 100 --throttle 300
-
连接设备
adb connect 127.0.0.1:62001/62026
-
查看已连接设备
adb devices
-
根据apk包查看包名和activity、udid | 查看已安装包
aapt dump badging apk
adb shell pm list packages
-
安装|启动|卸载
adb install apk -s指定seed,-r覆盖安装
adb shell am atart appPackage
adb uninstall appPachage
-
文件传输
adb pull f1 f2 手机文件外传
adb push f1 f2 外部文件传入手机
-
系统类
查看进程 adb shell ps | findstr packagename
杀死进程 adb shell kill pid
查cpu adb shell dumpsys cpuinfo | findstr packagename
查内存 adb shell dumpsys meminfo | findstr packagename查系统内存|cpu|网络情况 adb shell cat /proc/meminfo [cpuinfo|net]
电量 adb shell dumpsys battery
设备信息 adb shell getprop (手机名称、型号、系统版本、屏幕大小、分辨率、手机号等)
Appium
介绍:appium封装了标准selenium库,扩展了WebDriver的协议,能直接继承之前的WebDriver API
安装:pip install Appium-Python-Client -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
from appium import webdriver
desired = {}
desired['platformName'] = 'Android'
desired['platformVersion'] ='8.1.0'
desired['deviceName'] = 'android'
desired['appPackage'] = 'com.mobivans.onestrokecharge'
desired['appActivity'] = 'com.mobivans.onestrokecharge.activitys.BootActivity'
desired['uuid'] = '127.0.0.1:62001'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired) #4723:appium server默认监听端口
appium定位
appium定位和操作借助于三方工具,如android sdk、UISpy,定位工具控件于Android SDK 的 /tools/bin/目录下
appium 通过 uiautomatorviewer.bat 工具查看控件属性
定位方式 | 实现 |
---|---|
id | driver.find_element_by_id() |
accessibility_id | driver.find_element_by_accessibility_id() |
name | driver.find_element_by_name() |
class_name | driver.find_element_by_class_name() |
xpath | driver.find_element_by_xpath() |
android_uiautomator | driver.find_element_by_android_uiautomator(‘newUiSelector().text()’) |
driver.tap() | 基于坐标 |
appium操作
操作 | 实现 |
---|---|
安装app | 安装appdriver.install_app(‘D:\XX.apk’) |
卸载 | driver.remove_app(‘com.example.android.apis’) |
启动 | driver.launch_app() |
关闭 | driver.close_app() #默认关闭当前应用,无参 |
app是否安装 | driver.is_app_installed(‘com.example.android.apis’) |
app置于后台 | driver.background_app(2) #2-时长 |
重置app | driver.reset() |
熄屏 | driver.lock(3) # 3秒 |
收起键盘 | driver.hide_keyboard() (当对输入框输入完成后需将键盘收起) |
滑动 | driver.swipe(75, 500, 75, 0, 800) 原坐标→目的坐标,作用时间 |
当前Activity | print(driver.current_activity) |
值 | 单击 | send_keys() | click() |