1 概述
在之前的内容上我们已经知道如何在python中使用adb和shell。
但是实际上每个命令一直要全部手打那是很麻烦。
今天对这一部分的内容进行优化。
2 测试的全局变量
见上一篇读取基础配置的内容:
python自动化入门
我们这边直接基于上述写好的read.py使用。
from read import readConfig #导入包的操作
...
a = testInfo()
...
class testInfo: #testInfo 类用来存储测试模式/测试次数/测试设备
def __init__(self):
self.testCase = ""
self.count = 0
self.devices = ""
...
testInfo = readConfig() #读取config.txt文件
apk = testInfo[0] #测试apk名字
a.count = testInfo[1] #测试次数
a.testCase = testInfo[2] #测试模块
a.devices = testInfo[3] #测试设备,这边测试设备是第三个字段.我第二个字段的内容是测试的模式/open/photo
3 ADB
写一个adb命令操作函数,这个函数需要返回adb相关命令的操作。
本质上就是字符之间的拼接。
#adb 命令函数
def adbCmd(cmd):
global a
ACmd = "adb -s "+a.devices +" "+ cmd
print(ACmd)
result = os.popen(ACmd)
# print("执行命令后返回的内容为")
#print(result.read())
return result.read()
写另外的adb操作函数不返回值。
def adbCmd2(cmd):
global a
ACmd = "adb -s "+a.devices +" "+ cmd
print(ACmd)
os.system(ACmd)
我写两个的目的也只是为了让直接方便操作。毕竟不是每一次都需要返回操作的内容。
然后写拍照返回home等相关函数
# 获取adb 设备信息
def getDevices():
global a
devices = os.popen("adb devices|awk '{print $1}'|sed -n '2p'")
a.devices = devices.read()
a.devices = a.devices.replace("\n","") #删掉换行符
# 获取hal的pid
def getPid():
global halPid
cmd = adbCmd("shell ps -A |grep \"camerahalserver\" |awk '{print $2}'|sed -n '1p'") #获取camerahalserver的PID
halPid = cmd.replace("\n","")
print("halPid = ",cmd)
#返回home函数
def goHome():
adbCmd("shell input keyevent 3")
#mode = 2 打开相机
def openCamera():
cmd = "shell am start -W "+apk
adbCmd(cmd)
#mode = 3 拍照
def photo():
adbCmd2("shell input keyevent 27")
4 内存监控
在了解以上的情况下,我们进行一个拓展,写一个监控android设备camera的内容情况。
adb -s $devices shell dumpsys meminfo camerahalserver |grep -iE "TOTAL PSS" #PSS
adb -s $devices shell cat sys/kernel/debug/ion/ion_mm_heap #ION
adb -s $devices shell cat /proc/$pid/smaps #SMAP
整个测试架构如下:其中每个部分都可以单独拆解出来写一个包方便继承使用。
监控手机打开测试4469次,显示如上,内存非常的好,没有明显的变动。无内存泄露。