安卓&iOS模拟点击器

17 阅读1分钟

背景

对于一些重复执行很多次才偶现的问题,研发修复之后,进行回归验证,很费手指,每次手指都要点费了,才能得出一个结论:已修复未复现。

所以,怎样才能自动点击呢?

方案一:脚本(仅支持安卓)

把点击坐标记录下来,脚本中循环执行adb shell input ***

import os
import time


def adb_touch_loop(positions: list, wait_time=0.5, touch_times=100):
    for i in range(touch_times):
        print(f'第{i+1}次点击')
        for pos in positions:
            print(f'-- 坐标点: {pos}')
            if len(pos) == 2:
                # 单次点击: (x, y)
                os.system("adb shell input tap %d %d" % (pos[0], pos[1]))
            elif len(pos) == 4:
                # 滑动: (x1, y1, x2, y2) 由坐标(x1, y1)滑动到(x2, y2)
                os.system("adb shell input swipe %d %d %d %d 300" % (pos[0], pos[1], pos[2], pos[3]))
            else:
                print('坐标点格式有误!')
                break
            time.sleep(wait_time)
    print('>>> END <<<')


if __name__ == '__main__':
    adb_touch_loop(positions=[(860, 1800), (600, 1900, 600, 1000), (500, 400)], wait_time=0.5, touch_times=5)

方案二:工具(支持安卓、iOS)

iOS需安装WDA

详见click-boy

截图后,点击图片可记录坐标,2个坐标可以设置滑动(Event为Swipe)或其他操作。

选好需要循环点击的坐标后,设置点击时间间隔、循环次数,点击Run开始执行。

image.png

注意:mac安装产出会报文件已损坏错误,原因是没有经过苹果官方公证(99刀每年),可将源码下载下来自行编译。