本文主要是介绍Monkeyrunner+Python+真机 多台手机按顺序安装设置app,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# coding:utf-8
import os
# import subprocess
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.easy import By
from com.android.monkeyrunner.easy import EasyMonkeyDevice as emd
# 工具类 有两个方法
# getDeviceServial:读取保存devices,并保存到devices[]
# viewServerisOpen:判断viewserver是否可用
class Tools( object):
def __init__( self, deviceServial):
self.deviceServial = deviceServial
# ************************************************************************
# 判断手机 是否打开viewserver 如果打开 可以使用EasyMonkeyDevice
# ************************************************************************
def viewServerIsOpen( self):
# print(self.deviceServial)
command = "adb -s "+ self.deviceServial
viewServer = os.system(command + " shell service call window 3")
# viewServer = subprocess.getstatusoutput(
# command + " shell service call window 3")
# print(viewServer)
# if viewServer[0] == 1:#monkeyrunner 不支持该方法
if viewServer == 1:
# print("你的手机已经打开viewServer")
return True
else:
# print("开启ViewServer")
ovs = os.system(command+ " shell service call window 1 i32 4939")
# ovs = subprocess.getstatusoutput(
# command+" shell service call window 1 i32 4939")
# print(ovs)
# if ovs[0] == 1:
if ovs == 1:
# print("你的手机已经打开viewServer")
return True
else:
# print("你的手机无法打开viewserver 无法使用EasyMonkeyDevice")
return False
# ************************************************************************
# 获取所有的设备 返回所有设备的数组
# ************************************************************************
@ classmethod
def getDeviceServial( self):
print( "获取所有的devices,并返回devices[]")
# 创建一个数组用来存放devices
devices = []
# 将所有的devices 写入devices.text
# devicesPath = str(os.getcwd())
devicesPath = "D:/PythonProject/MonkeyRunner"
os.system( "adb devices > "+devicesPath+ "/a3s/devices.text")
# 读取devices.text
f = open(devicesPath+ "/a3s/devices.text", "r")
content = f.readlines()
i = 1 # 因为第一行没有device的信息 所以下标从1开始,而且最后一行空白的所以i要小于len-1
# print(len(content))
while i < len(content)- 1:
# 找到空格的位置或者有时识别不出空客 用device
# findNumber = content[i].find(" ")
findNumber = content[i].find( "device")- 1
# print(findNumber)
# 截取空格之前的字符串保存到devices[]
devices.append(content[i][ 0:findNumber])
i += 1
# 读取所有的devices
for device in devices:
print(device)
return devices
# 方法1:通过monkeydevice的installPackage 安装appp
def installApp( device):
print( "begin install app by monkeyrunner")
# 安装jkt
device.installPackage( "D: \\ JKT \\ apks \\ JKT_V1.2.24-release.apk")
print( "安装jkt--ok")
# 安装weike2.1.16
device.installPackage( "D: \\ JKT \\ apks \\ Weike_V2.1.16-release.apk")
print( "安装weike--ok")
# 安装test
device.installPackage(
"D: \\ JKT \\ apks \\ Weike_V2.1.10-debug-androidTest.apk")
print( "安装test--ok")
# 安装wechat6.6.5
device.installPackage( "D: \\ JKT \\ apks \\ Wechat_V6.6.5.apk")
print( "wechat--ok")
print( "install app end ")
# 设置supersu
def setUpSperSU():
print( "***********************supersu begining**************************")
device.startActivity(
component= "eu.chainfire.supersu/eu.chainfire.supersu.MainActivity-Material")
mr.sleep( 1)
# 点击设置
device.touch( 600, 210, "DOWN_AND_UP")
mr.sleep( 1)
# 点击重新验证
device.touch( 500, 900, "DOWN_AND_UP")
mr.sleep( 1)
# 点击默认操作
device.touch( 200, 1100, "DOWN_AND_UP")
mr.sleep( 1)
# 点击默认操作--授权
device.touch( 200, 650, "DOWN_AND_UP")
# 截图并与标准图进行比较
# standardImage=mr.loadImageFromFile("D:\\PythonProject\\MonkeyRunner\\a3s\\standard_images\\supersu_1.png") #标准图
mr.sleep( 2)
# image=mi.getSubImage(0,0,720,1080) #指定矩形截图
# device.takeSnapshot().writeToFile(
# "D:\\PythonProject\\MonkeyRunner\\a3s\\standard_images\\supersu_1.png") # 截图并保存
# image=mr.loadImageFromFile("D:\\PythonProject\\MonkeyRunner\\a3s\\standard_images\\supersu_1_1.png")#比较
# 实际操作中,因为截图中的时间不一致 会一直报错
# if mi.sameAs(standardImage,image):
# print("all right!")
# else:
# mr.alert("something wrong!!")
# 下滑
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
# 点击开机允许所有授权申请
device.touch( 500, 900, "DOWN_AND_UP")
mr.sleep( 1)
# device.takeSnapshot().writeToFile(
# "D:\\PythonProject\\MonkeyRunner\\a3s\\standard_images\\supersu_2.png") # 截图并保存
# 下滑
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
device.drag(( 500, 1000), ( 500, 400), 2, 10)
mr.sleep( 0.1)
# 点击开机允许所有授权申请
device.touch( 500, 600, "DOWN_AND_UP")
mr.sleep( 1)
# device.takeSnapshot().writeToFile(
# "D:\\PythonProject\\MonkeyRunner\\a3s\\standard_images\\supersu_3.png") # 截图并保存
print( "***********************supersu end**************************")
if __name__ == '__main__':
# 获取所有的devices[]
devices = Tools.getDeviceServial()
# 遍历devices,
for temp in devices:
tool = Tools(temp)
# 判断能否使用EasyMonkeyDevice
if tool.viewServerIsOpen():
# 能使用
print( "viewServer is opened!")
else:
print( "Can't open viewServer ")
# print("link device")
device = mr.waitForConnection( 1.0, temp)
# print("install app by monkeydevice")
installApp(device)
# print("setup supersu by monkeyrunner")
setUpSperSU()
这篇关于Monkeyrunner+Python+真机 多台手机按顺序安装设置app的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!