1膝蜈、目的介紹
本文主要通過Python 的os.popen()方法執(zhí)行adb命令锅移,進(jìn)行封裝非剃,達(dá)到自動化測試指定APP的冷啟動备绽、熱啟動疯坤。指定執(zhí)行次數(shù)深浮,取對應(yīng)平均值飞苇。
冷啟動:啟動應(yīng)用時布卡,后臺沒有該應(yīng)用的進(jìn)程忿等,此時系統(tǒng)會重新創(chuàng)建一個新的進(jìn)程分配給該應(yīng)用贸街,這個啟動方式就是冷啟動狸相。
熱啟動:啟動應(yīng)用時脓鹃,后臺已有該應(yīng)用的進(jìn)程(如:操作HOME鍵回返回至桌面瘸右,此時該應(yīng)用的進(jìn)程仍會保留在后臺,可在任務(wù)列表內(nèi)查看)苞俘,故在已有進(jìn)程的情況下苗胀,該種啟動會在已有的進(jìn)程內(nèi)來啟動應(yīng)用,這個方式叫熱啟動歌亲。
ADB命令執(zhí)行:http://www.reibang.com/p/3c226477aa46
2陷揪、獲取待測APP的包名以及Activity
- 使用adb命令:adb shell dumpsys window | findstr "mCurrentFocus"
- 注:常用adb命令總結(jié):http://www.reibang.com/p/d6340b67d9e3
-
以手機(jī)QQ為例:連接手機(jī)悍缠,并進(jìn)入手Q主頁飞蚓,執(zhí)行adb命令獲取包名+activity
如圖可見趴拧,手Q包名:'com.tencent.mobileqq'山叮,主頁activity:'com.tencent.mobileqq.activity.SplashActivity'
3脑又、封裝腳本代碼
# -*- coding: utf-8 -*-
"""
@author: rzb
@software: PyCharm
@file: adbtest.py
@time: 2019/9/21 12:00
"""
import os
import re
import time
class MobileQQTest(object):
def __init__(self, num):
self.data = []
self.num = num
self.app = 'com.tencent.mobileqq'
self.activity = 'com.tencent.mobileqq.activity.SplashActivity'
def check_devices(self):
'''檢查設(shè)備是否連接成功问麸,如果成功返回True口叙,否則返回False'''
try:
deviceInfo = os.popen('adb devices').read()
if 'device' in deviceInfo.split('\n')[1]:
print('=' * 21, '已連接設(shè)備,開始測試', '=' * 21)
print(self.deviceInfo())
return True
else:
return False
except Exception as e:
print(e)
def deviceInfo(self):
'''獲取設(shè)備基礎(chǔ)信息(如:廠商妄田、型號驮捍、系統(tǒng)版本)'''
deviceName = os.popen('adb shell getprop ro.product.model').read()
platformVersion = os.popen('adb shell getprop ro.build.version.release').read()
producer = os.popen('adb shell getprop ro.product.brand').read()
return "手機(jī)型號:%s %s东且,系統(tǒng)版本:Android %s" % (
producer.replace('\n', ''), deviceName.replace('\n', ''), platformVersion.replace('\n', ''))
def start_adb(self):
'''運(yùn)行adb命令珊泳,并記錄啟動耗時'''
start = 'adb shell am start -W %s/%s' % (self.app, self.activity)
data = re.findall(r'.*ThisTime: (.*?)TotalTime:(.*?)WaitTime: (.*?)Complete',
os.popen(start).read().replace('\n', ''))
if len(data) == 0:
print("adb命令執(zhí)行出錯,數(shù)據(jù)為空")
else:
self.data.append(int(data[0][0]))
return data
def stop_adb(self):
'''結(jié)束程序運(yùn)行'''
stop = 'adb shell am force-stop %s' % self.app
os.popen(stop)
def run_test_cold(self):
'''app 冷啟動耗時測試'''
self.data.clear()
if self.check_devices() == True:
self.stop_adb()
for i in range(self.num):
print('=' * 20, '冷啟動測試:第%d次運(yùn)行' % (i + 1), '=' * 20)
self.stop_adb()
time.sleep(3)
test = self.start_adb()
print("ThisTime:%s,TotalTime:%s,WaitTime:%s" % (test[0][0], test[0][1], test[0][2]))
time.sleep(3)
self.stop_adb()
print('\n冷啟動%s次平均耗時為:%s' % (len(self.data), sum(self.data) / len(self.data)))
else:
print("未連接安卓設(shè)備,請連接設(shè)備(3秒后重試)")
while True:
time.sleep(3)
self.run_test_cold()
def run_test_hot(self):
'''app 熱啟動耗時測試'''
self.data.clear()
if self.check_devices() == True:
os.popen('adb shell am start -W %s/%s' % (self.app, self.activity))
time.sleep(3)
for i in range(self.num):
print('=' * 20, '熱啟動測試:第%d次運(yùn)行' % (i + 1), '=' * 20)
os.popen('adb shell input keyevent 3')
time.sleep(3)
test = self.start_adb()
time.sleep(3)
print("ThisTime:%s,TotalTime:%s,WaitTime:%s" % (test[0][0], test[0][1], test[0][2]))
self.stop_adb()
print('\n熱啟動%s次平均耗時為:%s' % (len(self.data), sum(self.data) / len(self.data)))
else:
print("未連接安卓設(shè)備,請連接設(shè)備(3秒后重試)")
while True:
time.sleep(3)
self.run_test_hot()
if __name__ == '__main__':
apptest = MobileQQTest(4)
apptest.run_test_cold()
apptest.run_test_hot()
4、執(zhí)行結(jié)果展示
===================== 已連接設(shè)備,開始測試 =====================
手機(jī)型號:vivo V1824BA序无,系統(tǒng)版本:Android 9
==================== 冷啟動測試:第1次運(yùn)行 ====================
ThisTime:83,TotalTime: 83,WaitTime:114
==================== 冷啟動測試:第2次運(yùn)行 ====================
ThisTime:155,TotalTime: 155,WaitTime:188
==================== 冷啟動測試:第3次運(yùn)行 ====================
ThisTime:167,TotalTime: 167,WaitTime:197
==================== 冷啟動測試:第4次運(yùn)行 ====================
ThisTime:164,TotalTime: 164,WaitTime:193
冷啟動4次平均耗時為:142.25
===================== 已連接設(shè)備,開始測試 =====================
手機(jī)型號:vivo V1824BA帝嗡,系統(tǒng)版本:Android 9
==================== 熱啟動測試:第1次運(yùn)行 ====================
ThisTime:119,TotalTime: 119,WaitTime:148
==================== 熱啟動測試:第2次運(yùn)行 ====================
ThisTime:82,TotalTime: 82,WaitTime:105
==================== 熱啟動測試:第3次運(yùn)行 ====================
ThisTime:72,TotalTime: 72,WaitTime:92
==================== 熱啟動測試:第4次運(yùn)行 ====================
ThisTime:69,TotalTime: 69,WaitTime:92
熱啟動4次平均耗時為:85.5
Blog:
- 簡書:http://www.reibang.com/u/ec81abf35751
- CSDN:https://blog.csdn.net/qq_21238607
- 微信公眾號:rzbbzr