当时正在研究如何uiautomator的源码,看的头疼。和朋友聊天,朋友说monkey不想敲命令,反正也无聊,就花了2个小时写了个GUI。
最终呈现的效果图:
实际上是借助adb shell monkey的命令的拼接,需要的环境和依赖包安卓SDK自带的adb,python3.6和pyqt5.这里我们先封装一个monkey函数。
#-*-coding:utf-8-*- ''' @Author:zhang yiqian ''' import os def monkey(**kwargs): command="adb shell monkey" try: command+=" -p "+kwargs["packageName"] except: print ("没有指定包") command += " -v " try: command+=kwargs["count"] except: print("没有指定次数") try: command += " -s " + kwargs["seed"] except: print ("没有指定种子数") try: command += " --throttle " + kwargs["throttle"] if kwargs["random"]==True: command += " --randomize-throttle" else: pass except: print ("没有指定时间间隔") #print (command) report=os.popen(command).read() return report
然后我们去用pyqt5制作一个GUI,代码如下
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'monkey.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import sys,monkeyadb class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(346, 484) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(260, 50, 71, 81)) self.pushButton.setObjectName("pushButton") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(90, 50, 171, 21)) self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(Form) self.lineEdit_2.setGeometry(QtCore.QRect(90, 90, 171, 21)) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_3 = QtWidgets.QLineEdit(Form) self.lineEdit_3.setGeometry(QtCore.QRect(90, 70, 171, 21)) self.lineEdit_3.setObjectName("lineEdit_3") self.lineEdit_4 = QtWidgets.QLineEdit(Form) self.lineEdit_4.setGeometry(QtCore.QRect(90, 110, 171, 21)) self.lineEdit_4.setObjectName("lineEdit_4") self.lineEdit_5 = QtWidgets.QLineEdit(Form) self.lineEdit_5.setGeometry(QtCore.QRect(30, 50, 61, 21)) self.lineEdit_5.setObjectName("lineEdit_5") self.lineEdit_6 = QtWidgets.QLineEdit(Form) self.lineEdit_6.setGeometry(QtCore.QRect(30, 70, 61, 21)) self.lineEdit_6.setObjectName("lineEdit_6") self.lineEdit_7 = QtWidgets.QLineEdit(Form) self.lineEdit_7.setGeometry(QtCore.QRect(30, 90, 61, 21)) self.lineEdit_7.setObjectName("lineEdit_7") self.lineEdit_8 = QtWidgets.QLineEdit(Form) self.lineEdit_8.setGeometry(QtCore.QRect(30, 110, 61, 21)) self.lineEdit_8.setObjectName("lineEdit_8") self.checkBox = QtWidgets.QCheckBox(Form) self.checkBox.setGeometry(QtCore.QRect(30, 130, 141, 31)) self.checkBox.setObjectName("checkBox") self.textBrowser = QtWidgets.QTextBrowser(Form) self.textBrowser.setGeometry(QtCore.QRect(35, 161, 291, 301)) self.textBrowser.setObjectName("textBrowser") self.retranslateUi(Form) self.pushButton.clicked.connect(self.startmonkey) QtCore.QMetaObject.connectSlotsByName(Form) #编写自己的槽函数 def startmonkey(self): try: packageName=self.lineEdit.text() count=self.lineEdit_3.text() seed=self.lineEdit_2.text() throttle =self.lineEdit_4.text() random =self.checkBox.isChecked() report=monkeyadb.monkey(packageName=packageName,count=count,seed=seed,throttle=throttle,random=random) self.textBrowser.setText(report) except: print ("error") def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "执行")) self.lineEdit_5.setText(_translate("Form", "包名")) self.lineEdit_6.setText(_translate("Form", "事件次数")) self.lineEdit_7.setText(_translate("Form", "种子数")) self.lineEdit_8.setText(_translate("Form", "间隔时间")) self.checkBox.setText(_translate("Form", "间隔时间是否随机")) if __name__ == "__main__": app = QApplication(sys.argv) ex = Ui_Form() w = QMainWindow() ex.setupUi(w) w.show() sys.exit(app.exec_())
编写槽函数,通过信号和槽建立连接:
self.pushButton.clicked.connect(self.startmonkey) #编写自己的槽函数 def startmonkey(self): try: packageName=self.lineEdit.text() count=self.lineEdit_3.text() seed=self.lineEdit_2.text() throttle =self.lineEdit_4.text() random =self.checkBox.isChecked() report=monkeyadb.monkey(packageName=packageName,count=count,seed=seed,throttle=throttle,random=random) self.textBrowser.setText(report) except: print ("error")
这样我们的monkey工具就完成了,然后因为朋友是妹子,不会装python环境和pyqt5,我就把python文件打包成exe文件,这样就只需要安卓SDK了。
1、使用pyinstaller
step1:安装pyinstaller,在cmd窗口使用pip install pyinstaller安装
step2:cd 到你的文件目录cd E:pjmonkey
step3:运行pyinstaller -F monkey.py,注意-F是大写
step4:看结果,结果在新生成的文件夹dist下就有monkey.exe
补充一下后来发现的问题,运行pyinstaller -F monkey.py后的exe打开时总是先有一个cmd窗口出现,要去掉的话应该用运行pyinstaller -F -w monkey.py