# -*- encoding=utf8 -*-
__author__ = "yangqianghong"
from airtest.aircv import *
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest.core.settings import Settings as ST
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
if not cli_setup():
auto_setup(__file__, logdir=True, devices=["Android:///",])
ST.THRESHOLD = 0.5
#ST.SAVE_IMAGE=False
# for i in range(1,3): #截取不同的图片
# screen = G.DEVICE.snapshot(f"G:\Snapshot_test\log\%s.png"%i)
#
# screen = G.DEVICE.snapshot(r"G:\Snapshot_test\log\11.png",quality=99)
# local_screen = aircv.crop_image(screen,(0,160,1067,551))
# #try_log_screen(screen)
#
# template=Template(screen) #目标截图
# pos=template.match_in(local_screen)
# print(pos)
# screen = G.DEVICE.snapshot(r"G:\Snapshot_test\log\11.png")
# # 局部截图
# local_screen = aircv.crop_image(screen,(0,949,1067,1500))
# #try_log_screen(local_screen)
# # 将我们的目标截图设置为一个Template对象
# tempalte = Template(r"G:\Snapshot_test\log\11.png")
# # 在局部截图里面查找指定的图片对象
# pos = tempalte.match_in(local_screen)
#
# # 返回找到的图片对象的坐标(该坐标是相对于局部截图的坐标)
# print(pos)
def match_in_predict_area(template,screen=None,rect=None):
if screen is None:
screen=G.DEVICE.snapshots()
if rect is None:
return template.match_In(screen)
if not isinstance(rect,(list,tuple)):
raise Exception("crop a image,rect should be a list")
else:
predict_screen=aircv.crop_image(screen,rect)
focus_pos=template.match_in(predict_screen)
if not focus_pos:
return False
else:
return focus_pos[0]+rect[0],focus_pos[1]+rect[1]
def Mutiple_exists(targets,area=None,threshold=0.80,rgb=False,inti=5):
if (G.DEVICE.display_info['orientation']%2):
height=G.DEVICE.display_info['height']
width=G.DEVICE.display_info['width']
else:
height = G.DEVICE.display_info['height']
width = G.DEVICE.display_info['width']
for i in range(inti):
fullScreen=G.DEVICE.snapshot()
for targets in targets:
if targets:
focus_pos=match_in_predict_area(Template('%d\%s.png'%(width,targets),
threshold=threshold,rgb=rgb),
fullScreen,area)
if focus_pos:
ref=targets.index(targets)
return ref,focus_pos
sleep(0.2)
return -1
airtest 截图和多图查找
最新推荐文章于 2024-07-31 12:12:39 发布
这篇博客介绍了如何利用Python的Airtest和Poco库进行Android设备的UI自动化测试。通过设置Poco对象,截取屏幕快照并进行图像处理,实现模板匹配和定位。同时,定义了`match_in_predict_area`和`Mutiple_exists`两个函数,用于在特定区域查找图像模板并检测多个元素的存在。博客内容主要涉及自动化测试和移动应用开发。
摘要由CSDN通过智能技术生成