1.必须安装Android SDK,Python,JDK,unzip环境;
2.该脚本是依赖手机设备Nexus 5(脚本中的点击事件坐标是按照该设备设计的)
3.在当前目录创建apk文件夹,然后直接运行monkeyrunner ApkTest.py
------------世界如此喧嚣,真相何其稀少------------------------
2015.07.08
目录结构:
apk文件夹:需要测试渠道和验证微信的正式发布apk;
ApkTest.py:主要运行python脚本;
AXMLPrinter2.jar:从二进制XML文件中打印为可读的文件;
log文件夹:运行日志文件夹;
screenShot:存放着正确的basePic.png的标准图片(用来做微信验证),运行时,对比图片。
===============================================
apkUnzipFolder文件夹:利用unzip解压apk文件后存放的目录;
androidManifestTxt文件夹:利用AXMLPrinter2将二进制XML文件转化为可读的文件;
release_apk文件夹:没有意义的文件夹,不用理会。
#!/usr/bin/env monkeyrunner# -*- coding: utf-8 -*-#device:LGE Nexus 5import sysreload(sys)sys.setdefaultencoding("utf-8")import timeimport osfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage#设置应用包名和入口Activity名pakageName = 'com.XXX.XXX'componentName = pakageName + '/' +'com.XXX.XXXX.XXXActivity'#APP启动时等待时间(秒)startTime = 5#获取年月日时分秒now = time.strftime("%Y-%m-%d-%H-%M-%S")#python中获取当前运行的文件的名字name=sys.argv[0].split("\\")filename=name[len(name)-1]#MonkeyRunner下获取运行的文件所在的路径rootpath = os.path.split(os.path.realpath(sys.argv[0]))[0]#指定位置dir = rootpath + "/apk/"screenPath = rootpath + "/screenShot/"logpath = rootpath + "/log/"apkUnzipFolder = rootpath + "/apkUnzipFolder/" androidManifestTxt = rootpath + "/androidManifestTxt/"if not os.path.isdir(screenPath): os.mkdir(screenPath)if not os.path.isdir(apkUnzipFolder): os.mkdir(apkUnzipFolder)if not os.path.isdir(androidManifestTxt): os.mkdir(androidManifestTxt)#获取待测APK个数countPak = len(os.listdir(dir))#新建一个Log文件if not os.path.isdir(logpath): os.mkdir(logpath)print("logpath" +logpath)print("filename" + filename)print("filename" + filename[-10:-3])log = open( logpath + filename[-10:-3] + "-log" +now + ".txt" , 'w')#开始连接设备print("Connecting...")device = MonkeyRunner.waitForConnection()log.write("连接设备...\n")#卸载应用print('Removing...')device.removePackage(pakageName)print ('Remove Successful!')MonkeyRunner.sleep(2)log.write("初始化应用环境...\n")countOK = 0countNO = 0for i in os.listdir(dir): print('Installing...<%s>'%i) log.write("==========安装应用==========\n") path = dir + '//' + i log.write("开始解压") apkUzipdir = apkUnzipFolder + i if not os.path.isdir(apkUzipdir): os.mkdir(apkUzipdir) os.system("unzip -o -q -d " + apkUzipdir + " " + path) txtFileName = androidManifestTxt + i + ".txt" xmlFileName = apkUzipdir + "/AndroidManifest.xml" os.system("java -jar AXMLPrinter2.jar " + xmlFileName + " > " + txtFileName) print("java -jar AXMLPrinter2.jar " + xmlFileName + " > " + txtFileName) from xml.dom import minidom xmldoc = minidom.parse(txtFileName) itemlist = xmldoc.getElementsByTagName('meta-data') for s in itemlist : name = s.attributes['android:name'].value value = s.attributes['android:value'].value if name == 'UMENG_CHANNEL' : if(cmp(i[i.find("-")+1:i.rfind("-")], value) == 0): print(i[i.find("-")+1:i.rfind("-")]) print i + "--->>>" + value else: print(i[i.find("-")+1:i.rfind("-")]) print i + "--->>>" + value + "渠道标识错误" break #安装应用 device.installPackage(path) print('Install Successful!') #打开应用 device.startActivity(component=componentName) print("componentName:" +componentName) MonkeyRunner.sleep(startTime) log.write("启动App...\n") print("启动App...\n") #滑动屏幕4次 for j in range(1,4): device.drag((1000, 300), (0, 300), 0.1, 1) MonkeyRunner.sleep(1) #点击进入下一个页面 device.touch(536, 1526, "DOWN_AND_UP") MonkeyRunner.sleep(1) #点击微信icon device.touch(538, 1670, "DOWN_AND_UP") MonkeyRunner.sleep(5) #device.press("KEYCODE_DPAD_CENTER", "DOWN_AND_UP") # for x in range(1,4):# device.touch(530, 1022, "DOWN_AND_UP")# MonkeyRunner.sleep(1) # device.touch(530, 1022, "UP") # MonkeyRunner.sleep(5) #点击跳过 # device.touch(250, 1080, "DOWN_AND_UP") #到微信登录页面去 # device.startActivity(pakageName + "/" + pakageName + ".homepage.LoginActivity"); # print("到微信登录页面" + pakageName + "/" + pakageName + ".homepage.LoginActivity") # MonkeyRunner.sleep(startTime) #截图 result=device.takeSnapshot() print("Take ScreenShot...") #保存截图 result.writeToFile(screenPath + i + '.png','png') #进行图片比较 resultTrue=MonkeyRunner.loadImageFromFile(screenPath + r'basePic.png') print "Pic Comparing..." log.write("对比图片中...\n") if(result.sameAs(resultTrue,0.9)): print("%s is OK!"%i) log.write("比较通过!--%s--包可用!\n"%i) print("比较通过!--%s--包可用!\n"%i) #卸载应用 print('Removing...') log.write("初始化应用环境,移除中...\n") device.removePackage(pakageName) print ('Remove Successful!') log.write("==========移除完毕==========\n") countOK += 1 MonkeyRunner.sleep(2) else: print("False!Please check %s!"%i) log.write("比较失败!请检查安装包--%s--是否可用!\n"%i) print("比较失败!请检查安装包--%s--是否可用!\n"%i) breaklog.write("共测试 %s 个包,%d 个通过。"%(countPak,countOK))