python实现android流量数据自动化
如果项目组中需要你来做安卓手机app流量专项测试,这时候你会怎么做呢? 利用前人写的工具还是自己敲adb命令呢?
我选择自己敲adb命令:
安利两个adb命令
adb shell dumpsys package “被测试应用包名” | findstr userId"
adb shell cat /proc/net/xt_qtaguid/stats grep | findstr " + port
我的做法是利用u2来实现安卓手机UI自动化,每次执行完场景之后,再执行adb命令查询流量消耗数据。因为场景很多,要做到持久化,则需要将每次查询出来的结果,写入到excel中方便
数据整理。
(1)查端口、查流量数据
class Test(object):
def __init__(self):
os.popen('adb shell pm clear 包名') # 清除缓存
time.sleep(1)
lines = os.popen("adb shell dumpsys package 包名 | findstr userId") # adb 查看app内存
self.port = lines.read().split('=')[1] # 获取port
def get_Mk(self, text):
"""
获取流量,增量写入txt文件
"""
lines = os.popen("adb shell cat /proc/net/xt_qtaguid/stats grep | findstr " + self.port) # adb 查看app内存
result = lines.read()
with open(file_path + '/cs.txt', 'a') as fn:
fn.write(text + '\n')
for i in result:
fn.write(i)
以上两步是先请缓存然后,查端口,然后查流量消耗数据。并持续写入到txt文件中。
(2)新增场景:
def start_app(self):
"""
启动app
"""
self.get_Mk(text='初始值') # 查询初始值 (1)
d = u2.connect()
d.session('包名') # 启动app
time.sleep(1) # 等待完全加载进行adb命令进行查看
d(text=u'同意并进入').click()
self.get_Mk(text='启动APP') # 查询启动之后的值
(3)txt转化为有表头的excel
import openpyxl, os
file_path = os.path.normpath(
os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources")))
def WriteTable(input_txt, out_excel):
titles = (open(file_path + "/titles", "r").read()).split(",")
list3 = ["\t"]
for j in titles:
list3.append(j)
list2 = [list3]
data = (open(file_path + input_txt, "r").read()).split("\n")
for i in data:
list1 = []
for j in i.split(" "):
if j == "":
continue
else:
list1.append(j)
if not list1:
continue
try:
int(list1[0])
except:
list2.append(list1)
else:
list1.insert(0, "\t")
list2.append(list1)
# 打开文件
wb = openpyxl.load_workbook(file_path + "/result.xlsx")
sheet = ["Sheet1", "Sheet2", "Sheet3"]
for i in sheet:
ws = wb[i]
wb.remove(ws)
ws = wb.create_sheet(title="Pip")
excel_path = file_path + out_excel
for i in list2:
ws.append(i)
if os.path.exists(excel_path):
os.remove(excel_path)
wb.save(excel_path)
titles就是表头,在命令窗口中心执行adb命令可以查询到
如下:我加了逗号分割
idx,iface,acct_tag_hex,uid_tag_int,cnt_set,rx_bytes,rx_packets,tx_bytes,tx_packets,rx_tcp_bytes,rx_tcp_packets,rx_udp_bytes,rx_udp_packets,rx_other_bytes,rx_other_packets,tx_tcp_bytes,tx_tcp_packets,tx_udp_bytes,tx_udp_packets,tx_other_bytes,tx_other_packets
(4)如上所示,就是按照我们要求写入的excel表格,那么现在是需要怼他进行 计算并追加写入结果:
import os
import xlrd
from xlutils.copy import copy
file_path = os.path.normpath(os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources")))
xls = xlrd.open_workbook(file_path + '/aa.xlsx')
xlsx = copy(xls)
shtc = xlsx.get_sheet(0)
def write_data():
"""
对生成的excel表格进行实时计算,并写入
"""
book = xlrd.open_workbook(file_path + '/aa.xlsx') # 打开excel文件
sheet = book.sheet_by_index(0) # 通过索引打开第一个表
data = sheet.col_values(6) #这里只举例说明 发送的流量数据消耗rx_bytes
len_list = len(data)
for i in data:
if i == '':
data.remove(i)
data = map(lambda x: x.encode('utf-8'), data)
data.remove('rx_bytes')
print data
values = []
len_new_list = len(data)
for i in range(1, len_new_list - 2, 2):
value1 = round(((int(data[i + 2]) - int(data[i])) / 1024))
values.append(value1)
print values
len_values = len(values)
i = 7
y = 0
while i < len_list:
while y < len_values - 1:
shtc.write(i, 6, values[y])
i = i + 3
y = y + 1
xlsx.save(file_path + '/ssb.xlsx')
if __name__ == '__main__':
write_data()
前面一些操作是将查询出来的数据进行格式转化变为我们需要的list,再进行计算,然后写入到表格中。
步骤:安装apk或者清缓存-查端口-跑场景-查流量数据-写入txt-转化为excel-计算并追加写入结果。