airtest 截图和多图查找

这篇博客介绍了如何利用Python的Airtest和Poco库进行Android设备的UI自动化测试。通过设置Poco对象,截取屏幕快照并进行图像处理,实现模板匹配和定位。同时,定义了`match_in_predict_area`和`Mutiple_exists`两个函数,用于在特定区域查找图像模板并检测多个元素的存在。博客内容主要涉及自动化测试和移动应用开发。
摘要由CSDN通过智能技术生成
# -*- 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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值