老早就想写一个python的关于安卓测试的小工具了,奈何一直很懒…最近项目不忙打算把以前的遗憾全部补上!
小工具原理无外乎就是这样的,adb获取相关信息,然后通过string的相关api,或者正则表达式提取数据,最后输出到文件或者图表中,比方说某公司测试时延(也就是流畅度),通过input循环滑动屏幕,然后没滑动一次读取一次 /proc/忘记了的目录….最后计算出时延…
下面是常用的工具类啦
#!/usr/bin/env python
#_*_coding:utf-8_*_
import os
import re
import time
import pychartdir
import threading,thread
import urllib2,urllib
########################################################################
class Test_Utils:
"""封装了某个测试的相关工具"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
#连接adb,连接成功返回True
def connectADB(self, remoteIP):
""""""
cmd = "adb connect %s"%remoteIP
os.popen(cmd).read()
check_result = os.popen("adb get-state").read()
#一定要加.strip()去除空字符串,因为不保证adb返回给我们什么鬼
if check_result.strip() == 'device':
return True
else:
return False
#得到当前焦点的activity,返回名字
def getCurrentActivity(self):
""""""
cmd = "adb shell dumpsys activity top | grep ACTIVITY"
a_result = os.popen(cmd).read().strip()
pattern = re.compile(r'[\w|\d|.]+/[\w|\d|.]+')
return pattern.findall(a_result)[0]
#返回某个包的所有权限列表
def getAllPermission(self,pkgName):
""""""
cmd = "adb shell dumpsys package %s"%pkgName
p_result = os.popen(cmd).read().strip()
pattern = re.compile(r'android.permission.\w+')
return pattern.findall(p_result)
#以格式xx-xx-xx xx-xx-xx返回当前的时间
def getCurrentTime(self):
""""""
return time.strftime('%Y-%m-%d %H-%M-%S',time.localtime(time.time()))
#取得保存的carsh日志
def getCrash(self):
""""""
return os.popen('adb shell dumpsys dropbox --print').read()
#图标类:输入一个列表和路径以及x轴的步长,输出一个图表
def createXYChart(self,data_list,outPath,step = 1):
""""""
labels = [str(x) for x in range#!/usr/bin/env python
#_*_coding:utf-8_*_
import os
import re
import time
import pychartdir
import threading,thread
import urllib2,urllib
########################################################################
class Test_Utils:
"""封装了某个测试的相关工具"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
#连接adb,连接成功返回True
def connectADB(self, remoteIP):
""""""
cmd = "adb connect %s"%remoteIP
os.popen(cmd).read()
check_result = os.popen("adb get-state").read()
#一定要加.strip()去除空字符串,因为不保证adb返回给我们什么鬼
if check_result.strip() == 'device':
return True
else:
return False
#得到当前焦点的activity,返回名字
def getCurrentActivity(self):
""""""
cmd = "adb shell dumpsys activity top | grep ACTIVITY"
a_result = os.popen(cmd).read().strip()
pattern = re.compile(r'[\w|\d|.]+/[\w|\d|.]+')
return pattern.findall(a_result)[0]
#返回某个包的所有权限列表
def getAllPermission(self,pkgName):
""""""
cmd = "adb shell dumpsys package %s"%pkgName
p_result = os.popen(cmd).read().strip()
pattern = re.compile(r'android.permission.\w+')
return pattern.findall(p_result)
#以格式xx-xx-xx xx-xx-xx返回当前的时间
def getCurrentTime(self):
""""""
return time.strftime('%Y-%m-%d %H-%M-%S',time.localtime(time.time()))
#取得保存的carsh日志
def getCrash(self):
""""""
return os.popen('adb shell dumpsys dropbox --print').read()
#图标类:输入一个列表和路径以及x轴的步长,输出一个图表
def createXYChart(self,data_list,outPath,step = 1):
""""""
labels = [str(x) for x in range(0,len(data_list)-1)]
c = pychartdir.XYChart(250,250)
c.setPlotArea(30,20,200,200)
c.addLineLayer(data_list)
c.xAxis().setLabels(labels)
c.xAxis().setLabelStep(step)
c.makeChart(outPath)
#图标类:输入两个列表,其他重载上面那个方法
def createDoubleXYChart(self,data_list,data_list02,outPath,step = 1):
""""""
labels = [str(x) for x in range(0,len(data_list)-1)]
c = pychartdir.XYChart(600, 300, 0xeeeeff, 0x000000, 1)
c.setPlotArea(80,50,500,200)
c.addLegend(80,50,0,"arialbd.ttf",9) #就是添加左上角线的解释,必须写
c.addTitle("hello togic", "timesbi.ttf", 15)
c.xAxis().setLabels(labels)
c.xAxis().setLabelStep(step)
layer = c.addLineLayer()
layer.setLineWidth(2)
layer.addDataSet(data_list, 0xff0000, "test data 00")
layer.addDataSet(data_list02, 0x008800, "test data 01")
c.makeChart(outPath)
########################################################################
class CheckURL(threading.Thread):
""""""
#----------------------------------------------------------------------
def __init__(self,inURL):
"""Constructor"""
threading.Thread.__init__(self)
if "http://" in inURL:
self.inURL = inURL
else:
h = 'http://'
h+=inURL
self.inURL = h
def run(self):
try:
resp = urllib2.urlopen(urllib2.Request(self.inURL))
code = resp.code
if code < 304:
print 'url:-->%s<-- is True(%d)'%(self.inURL,code)
except Exception:
print 'url:-->%s<-- is False'%self.inURL
finally:
thread.exit_thread
########################################################################
class CheckListUrl():
""""""
#----------------------------------------------------------------------
def __init__(self,listUrl):
"""Constructor"""
for u in listUrl:
CheckURL(u).start()
if __name__ == '__main__':
#c = Test_Utils()
#print c.connectADB('192.168.6.106')
#print c.getCurrentActivity()
#print c.getAllPermission("com.togic.livevideo")
#print c.getCurrentTime()
#print c.getCrash()
#c.createXYChart([1,5,9,4,5,2,3,6,4,5],"/home/cloudhuan/桌面/%s"%"out.png")
#c.createDoubleXYChart([1,5,6,9,8,7,4,5,6],[9,6,2,3,4,5,8,9,2,1],"/home/cloudhuan/桌面/%s"%"out02.png")
CheckListUrl(['www.qq.com','ggg.baidu,cin','http://www.sina.com'])(0,len(data_list)-1)]
c = pychartdir.XYChart(250,250)
c.setPlotArea(30,20,200,200)
c.addLineLayer(data_list)
c.xAxis().setLabels(labels)
c.xAxis().setLabelStep(step)
c.makeChart(outPath)
#图标类:输入两个列表,其他重载上面那个方法
def createDoubleXYChart(self,data_list,data_list02,outPath,step = 1):
""""""
labels = [str(x) for x in range(0,len(data_list)-1)]
c = pychartdir.XYChart(600, 300, 0xeeeeff, 0x000000, 1)
c.setPlotArea(80,50,500,200)
c.addLegend(80,50,0,"arialbd.ttf",9) #就是添加左上角线的解释,必须写
c.addTitle("hello togic", "timesbi.ttf", 15)
c.xAxis().setLabels(labels)
c.xAxis().setLabelStep(step)
layer = c.addLineLayer()
layer.setLineWidth(2)
layer.addDataSet(data_list, 0xff0000, "test data 00")
layer.addDataSet(data_list02, 0x008800, "test data 01")
c.makeChart(outPath)
#最简单的接口测试类
class CheckURL(threading.Thread):
''' 每传入一个url,开启一个线程验证,自带有http://容错逻辑 '''
#----------------------------------------------------------------------
def __init__(self,inURL):
"""Constructor"""
threading.Thread.__init__(self)
if "http://" in inURL:
self.inURL = inURL
else:
h = 'http://'
h+=inURL
self.inURL = h
def run(self):
try:
resp = urllib2.urlopen(urllib2.Request(self.inURL))
code = resp.code
if code < 304:
print 'url:-->%s<-- is True(%d)'%(self.inURL,code)
except Exception:
print 'url:-->%s<-- is False'%self.inURL
finally:
thread.exit_thread
########################################################################
class CheckListUrl():
""" 传入一个列表,校验 """
#----------------------------------------------------------------------
def __init__(self,listUrl):
"""Constructor"""
for u in listUrl:
CheckURL(u).start()
if __name__ == '__main__':
#c = Test_Utils()
#print c.connectADB('192.168.6.106')
#print c.getCurrentActivity()
#print c.getAllPermission("com.togic.livevideo")
#print c.getCurrentTime()
#print c.getCrash()
#c.createXYChart([1,5,9,4,5,2,3,6,4,5],"/home/cloudhuan/桌面/%s"%"out.png")
#c.createDoubleXYChart([1,5,6,9,8,7,4,5,6],[9,6,2,3,4,5,8,9,2,1],"/home/cloudhuan/桌面/%s"%"out02.png")
CheckListUrl(['www.qq.com','ggg.baidu,cin','http://www.sina.com'])