airtest选定随机位置点击,自动化点击,自动化测试adb随机位置点击,有需要拿去改改

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *
from random import randint
class PorTouch(object):
    def __init__(self):
        # 1.TODO 连接
        auto_setup(__file__)
        # 2 生成随机位置
        pos = self.get_em()
        rectangle_pos = [i['rectangle'] for i in pos]
        # 3 生成后的随机数列表
        lis_pos = self.rand_pos(rectangle_pos)
        # 4.位置点击
        self.go_work(lis_pos)
    # 定位
    def get_em(self):
        """
        :return: AirProject_em
        """
        # [{'result': (538, 1423), 'rectangle': ((490, 1363), (490, 1483), (587, 1483), (587, 1363)), 'confidence': 0.9852163195610046},
        # {'result': (301, 1186), 'rectangle': ((253, 1126), (253, 1246), (350, 1246), (350, 1126)), 'confidence': 0.9825006723403931},
        # {'result': (775, 1186), 'rectangle': ((727, 1126), (727, 1246), (824, 1246), (824, 1126)), 'confidence': 0.9197724461555481},
        # {'result': (249, 1894), 'rectangle': ((201, 1834), (201, 1954), (298, 1954), (298, 1834)), 'confidence': 0.7261688709259033}]
        results = find_all(Template(r"tpl1662624311911.png", record_pos=(-0.002, -0.178), resolution=(1080, 2520)))
        return results

    # 随机x组偏移量
    def rand_pos(self,rectangle_pos):
        """
        :rectangle_pos (1,2,3,4)
        :return:(左上,左下,右下,右上)
        """
        # 结果有几组就生成几组随机数
        lis_pos = list()
        for i in rectangle_pos:
            # i = ((490, 1363), (490, 1483), (587, 1483), (587, 1363))
            x = randint(int(i[0][0]),int(i[3][0]))
            y = randint(int(i[0][1]),int(i[1][1]))
            lis_pos.append((x,y))
        return lis_pos

    # 开始点击所有
    def go_work(self,tup_pos):
        for i in tup_pos:
            # TODO 点击每个位置
            touch(i)

if __name__ == '__main__':
    PorTouch()

 

posted @ 2023-10-25 17:12  trysocket  阅读(126)  评论(0编辑  收藏  举报