逍遥模拟器python控制命令
命令根据D:\Microvirt\MEmu\下面的MEmuConsole.exe来执行的
测试截图100次发非九秒多有点慢.输入100次用2秒多
import os
import random
import socket
import time
import cv2 as cv
import numpy as np
import read_file
import writelog
import uiautomator2 as u2
class MEmuConsole:
def __init__(self):
self.console = 'D:\\Microvirt\\MEmu\\'
self.screen_path = 'C:\\Users\\yys53\\OneDrive\\python\\bestscript\\screen_picture\\'
self.find_pic_path = 'C:\\Users\\yys53\\OneDrive\\python\\bestscript\\find_picture\\xiaoyao\\'
# 如果ret = 0,没有返回值,等于1有返回值
def console_input(self, cmd_console, ret=0):
cmd = self.console + cmd_console
process = os.popen(cmd)
if ret == 1:
result = process.read()
return result
else:
pass
# 启动指定模拟器,如0,1,2
def launch(self, index: int):
result = self.console_input('memuc start -i %d' % index, 1)
return result
# 关闭指定模拟器
def quit(self, index: int):
result = self.console_input('memuc stop -i %d' % index, 1)
return result
# 获取所有运行的模拟器
def all_info(self):
result = self.console_input('memuc listvms --running', 1)
return result
# 截图adb命令,不用silence增加速度<720*1270分辨率截图100次约7.17秒>
def screen(self, index: int, pic_name):
self.console_input('memuc -i %d adb "shell /system/bin/screencap -p /sdcard/Pictures/%s"' % (index, pic_name))
# 点击,adb命令"shell input tap %d %d" % (x, y)(点击100次约7.8秒)
def click(self, index: int, x, y):
self.console_input('memuc -i %d adb "shell input tap %.2f %.2f"' % (index, x, y))
# 输入文字:输入100次2.78秒
def input_text(self, index: int, text: str):
self.console_input('memuc input -i %d "%s"' % (index, text))
# 打开APP
def run_aap(self, index: int, name: str):
self.console_input('memuc startapp -i %d %s ' % (index, name))
# 停止app
def stop_app(self, index: int, name: str):
self.console_input('memuc stopapp -i %d %s ' % (index, name))
# 获取当前运行app名字和activity ,adb命令,shell shell dumpsys window | grep mCurrentFocus
def get_app_name(self, index: int):
result = self.console_input('memuc -i %d adb "shell dumpsys window | grep mCurrentFocus"' % index, 1)
return result
# 返回
def back(self, index: int):
self.console_input('memuc sendkey -i %d back' % index)
找元素和图
# 找元素和一张或多张图['图片名字,注意一个名字就可以找到多张前缀相同,后缀是1,2,3,4结尾图片']如果不加延迟找100次约7.04秒秒
def multiple_find_picture(self, index, img, times, click="点击", back="不返回", elem=()):
"""
#循环找图或单次找图
:param index: 模拟器从0开始
:param img: 图片名字,不带后缀
:param times: 找图次数
:param click:是否需要点击图片
:param back:按返回键一次
:param elem: elem元素查找
"""
# 记录超时时间
hostname = socket.gethostname()
start_time = time.time()
# print("开始找图:%s,第一次获取总秒数=%d" % (img, start_time))
i = 1
while i <= times:
delay = random.randint(1, 5) / 10
time.sleep(delay)
# 方法:用memuc调用adb截图,hostname,电脑名字
self.screen(index, 'xiaoyao_%d_%s.jpg' % (index, hostname))
# time.sleep(delay)
if elem: # 查找元素
for k in elem:
# print(k)
try:
# x和y传送两个值,所以可以这样写
a = self.d.xpath('//*[@' + k + ']')
b = a.wait(timeout=delay)
# print(a)
# a = self.d(text=k).wait(timeout=delay)
if b:
end_time = time.time()
spend_time = end_time - start_time
(x1, y1, x2, y2) = a.match().bounds
x = (x1 + x2) / 2
y = (y1 + y2) / 2
print("[逍遥模拟器%d]找到元素%s坐标:(%.2f,%.2f)本次找元素花费秒数=%.2f秒" % (index, k, x, y, spend_time))
writelog.write_file(
"[逍遥模拟器%d]找到元素%s坐标:(%.2f,%.2f)本次找元素花费秒数=%.2f秒" % (index, k, x, y, spend_time))
if click == "点击":
a.click()
return [x, y], elem.index(k)
except Exception as result:
# print("未知错误%s" % result)
pass
if img: # 查找图片
for k in range(len(img)):
imgobj = img[k]
len_pic = read_file.file_list(self.find_pic_path, "(^" + imgobj + "\\d+.\\w+)") # ^是字符串开头
# print(len_pic)
if len_pic: # 正则遍历图片个数,找几张图:图片都是名字+数字组成
# print(len(len_pic))
for j in range(0, len(len_pic)):
pic = len_pic[j][0]
# print(pic)
try:
# x和y传送两个值,所以可以这样写
imgsrc = self.screen_path + 'xiaoyao_%d_%s.jpg' % (index, hostname) # 第几个模拟器就第几个截图
imsrc = cv.imread(imgsrc) # ,1表示原图加载彩色图片,0以灰度模式,包括alpha,可以直接写-1
imobj = cv.imread(self.find_pic_path + pic)
h, w = imobj.shape[:2] # 取彩色图片的高、宽
res = cv.matchTemplate(imsrc, imobj, cv.TM_CCOEFF_NORMED)
threshold = 0.9
# 取匹配程度大于%90的坐标
loc = np.where(res >= threshold)
# np.where返回的坐标值(x,y)是(h,w),注意h,w的顺序
for pt in zip(*loc[::-1]):
x, y = pt[0] + w / 2, pt[1] + h / 2 # 中心点
# print("x=%d,y=%d" % (x, y))
if int(x) and int(y) >= 0:
print("[逍遥模拟器%d]找到图片%s坐标:x=%s,y=%s" % (index, pic, x, y))
writelog.write_file("[逍遥模拟器%d]找到图片%s坐标:x=%s,y=%s" % (index, pic, x, y))
time.sleep(delay)
if click == "点击":
self.click(index, x, y)
return [x, y], k
except Exception as result:
# print("未知错误%s" % result)
pass
# print("正在查找:%s,第%s次失败,正在进行%s次,第%d张" % (imgobj, i, i + 1, j + 1))
if back == "返回":
print("返回一次")
time.sleep(2.5)
self.back(index)
i += 1
else:
end_time = time.time()
spend_time = end_time - start_time
print("|找图超过规定次数|,模拟器%d,第%s次真的找不到%s,本次找图花费秒数=%.2f秒" % (index, i, img, spend_time))
if i > 10:
writelog.write_file("|找图超过规定次数|,模拟器%d,第%s次真的找不到%s,本次找图花费秒数=%.2f秒" % (index, i, img, spend_time))
return [-1, -1], -1
评论列表 (0 条评论)