平时大家做手机的自动化测试时,用得多的可能是uiautomator/uiautomator2这两个lib, 但是有时只是做一些简单的自动化操作来减轻人力的不足时,大可不必上牛刀,仅用简单的adb命令反而速度更快.
常用的cmd command整理如下:
//这条命令相当于按了设备的Backkey键
adb shell input keyevent 4
//可以点亮屏幕,相当于power key
adb shell input keyevent 26
//在屏幕上做划屏操作,前四个数为坐标点,后面是滑动的时间(单位毫秒)
adb shell input swipe 50 250 250 250 500
//在屏幕上点击坐标点x=50 y=250的位置
adb shell input tap 50 250
//输入字符abc
adb shell input text abc
//查看某个app是否已运行(当前是文件管理器)
adb shell ps | findstr filemanager
//强制关闭指定的app
adb shell am force-stop com.android.filemanager
//运行指定的app
adb shell am start com.android.filemanager/.FileManagerListActivity
本文指在对说明指摸拟特理按键/点击/滑屏/输入字符/运行app这些常cmd进行封装。
封装后的好处要求有以下几点:
1. 当调用这些func时,可以找印出当前的funcname, 做到一目了然。
2. 可以应对一台或多台手机时的同时调用,因为adb命令对于同时连接多台手机时是需要用-s 指定设备id的.
具体封装如下:
import os
from time import sleep
import time
# 执行普通的cmd命令
def exeCmd(cmdInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell ' + cmdInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell ' + cmdInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:exeCmd():{cmdInfo[1]}.')
return False
else:
print(f'设备{deviceid}:exeCmd():{cmdInfo[1]}.')
return True
# 按键动作
def pressKeyevent(keycodeInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell input keyevent ' + keycodeInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell input keyevent ' + keycodeInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:pressKeyevent():{keycodeInfo[1]}.')
return False
else:
print(f'设备{deviceid}:pressKeyevent():{keycodeInfo[1]}.')
return True
# 点击屏幕的动作
def clickScreen(positionInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell input tap ' + positionInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell input tap ' + positionInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:clickScreen():{positionInfo[1]}.')
return False
else:
print(f'设备{deviceid}:clickScreen():{positionInfo[1]}.')
return True
# 输入文本信息
def inputText(tTextInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell input text ' + tTextInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell input text ' + tTextInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:inputText():{tTextInfo[1]}.')
return False
else:
print(f'设备{deviceid}:inputText():{tTextInfo[1]}')
return True
# 滑动屏幕的动作
def swipeScreen(positionInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell input swipe ' + positionInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell input swipe ' + positionInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:swipeScreen():{positionInfo[1]}.')
return False
else:
print(f'设备{deviceid}:swipeScreen():{positionInfo[1]}')
return True
# 运行app
def launchApp(appactivityInfo, deviceid = ''):
if deviceid == '':
cmd = 'adb shell am start ' + appactivityInfo[0]
else:
cmd = 'adb -s ' + deviceid + ' shell am start ' + appactivityInfo[0]
if os.system(cmd) != 0:
print(f'设备{deviceid}:launchApp():{appactivityInfo[1]}.')
return False
else:
print(f'设备{deviceid}:launchApp():{appactivityInfo[1]}')
return True
然后怎么使用呢?用func的封装可以看出,传入的cmdinfo是List/tuple形式的。实例如下:
example:
# 贴上上面的封装func. 此处省略若干字... ...
if __name__ == '__main__':
# 这两个cmd的目的已经一目了解了,就是操作手机点亮屏幕并解锁.
lightCmd = ('26', '亮屏')
unlockscreen = ('380 1300 380 300', '解锁')
# 因为当前只有一台设备,故不用指定device id。
pressKeyevent(lightCmd)
swipeScreen(unlockscreen)
>>> 运行结果:
设备:pressKeyevent():亮屏
设备:swipeScreen():解锁
从结果一看,就知道哪台手机在执行哪个动作,当前在调用哪个函数,就一目了然了,当出现问题时,也好排查问题点了。
OK, 任务完成。
详细的常用cmd封装可参见:
https://github.com/vitamincqb/Toolset/blob/master/cmdutilsforphone.py
另外两个大神的lib可参见: