1、安装adb
网上安装adb驱动的文档比较多,就不多赘述了。
Mac:使用homebrew安装adb比较方便。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2、常用的adb命令
(1)使用adb安装apk指令:
adb devices
#查看连接设备
adb install <file>
#安装文件到手机 <file>为文件绝对路径,mac可以直接拖入该文件。
adb uninstall <pkg-name>
#根据包名卸载app
adb install -r <file>
#覆盖安装app
(2)adb启动优化指令
冷启动:指app被后台杀死后,首次启动APP消耗的时间。
热启动:指app没有被后台杀死, 按下返回键时回到桌面,在后台运行,再启动APP。
#打开指定app的指定启动页面
adb shell am start -W [packageName/activity]
#获得设备当前的activity名
adb shell dumpsys activity activities | grep 'mResumedActivity'
停止adb启动命令:
adb shell am force-stop [pacgageName]
3、app启动测试实例
为了方便,可以整合命令到脚本实现插上测试机即可使用, 使用时只需要更改包名称/启动页。
# /usr/bin/python
# encoding:utf-8
import csv
import time
import subprocess
import threading
import os
deviceId = ""
def cmd(s):
if deviceId.count == 0:
shell_str = "adb -s " + deviceId + " " + s
else:
shell_str = "adb " + s
return subprocess.Popen(shell_str, shell=True, stdout=subprocess.PIPE, bufsize=-1)
class App(object):
def __init__(self):
self.content = ""
self.thisTime = 0
self.totalTime = 0
self.waitTime = 0
self.activity = ""
def ShellCmd(self, s):
proc = cmd("shell " + s)
return proc.stdout
# 启动App
def LaunchApp(self):
cmd("logcat -c")
self.content = self.ShellCmd("am start -W PackageName/ActivityPage")
#PackageName/ActivityPage 修改为实际项目的名称
def monkey(self):
self.ShellCmd(
"CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar:/sdcard/fastbot-thirdpart.jar exec app_process /system/bin com.android.commands.monkey.Monkey -p PackageName --agent reuseq --running-minutes 10 --throttle 500 -v -v")
# 停止App
def StopApp(self):
self.ShellCmd("am force-stop PackageName") #冷启动
#self.ShellCmd("input keyevent 3") #热启动
#self.ShellCmd("pm clear PackageName") # 清理数据
# cmd = 'adb -s 72571231 shell pm clear PackageName'
# 获取启动时间
def GetLaunchedTime(self):
for line in self.content.readlines():
if "ThisTime" in line:
self.thisTime = line.split(":")[1].strip()
if "TotalTime" in line:
self.totalTime = line.split(":")[1].strip()
if "WaitTime" in line:
self.waitTime = line.split(":")[1].strip()
if "Activity" in line:
self.activity = line.split(":")[1].strip()
print("activity:" + self.activity + " time:(" + str(self.thisTime) + "," + str(self.totalTime) + "," + str(
self.waitTime) + ")")
display = ""
displayTime = ""
for line in cmd("logcat -d").stdout.readlines():
if "ActivityManager: Displayed" in line or "ActivityTaskManager: Displayed" in line:
t = line.split("Displayed")[1].strip()
ss = t.split(":")
display = ss[0].strip()
displayTime = ss[1].strip()
print("display:" + display + " displayTime:" + displayTime)
return (
self.getCurrentTime(), self.activity, self.thisTime, self.totalTime, self.waitTime, display, displayTime)
# 获取当前的时间戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return currentTime
# 控制类
class Controller(object):
def __init__(self, count):
self.app = App()
self.counter = count
self.alldata = [("timestamp", "Activity", "ThisTime", "TotalTime", "WaitTime", "display", "displayTime")]
# 单次测试过程
def testprocess(self):
self.app.LaunchApp()
time.sleep(3)
self.alldata.append(self.app.GetLaunchedTime())
time.sleep(3)
# self.app.monkey()
self.app.StopApp()
# 多次执行测试过程
def run(self):
self.app.StopApp()
time.sleep(3)
while self.counter > 0:
self.testprocess()
self.counter = self.counter - 1
time.sleep(3)
# 获取当前的时间戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return currentTime
# 数据的存储
def SaveDataToCSV(self):
csvfile = file('filename.csv', 'wb') #文件名称可改
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close()
if __name__ == "__main__":
controller = Controller(20) # 执行次数
controller.run()
controller.SaveDataToCSV()