侧边栏
Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.

python pyautogui 自动测试

# coding=utf-8
# 导入一些python包
import pyautogui
import cv2
import os
import time
import sys
import re
import functools


# str = 'abc'
# print(str[2:])
# exit()

# 正则表达式测试
# print(re.match('\d{1,}\.png', '1.png') is not None)
# print(re.match('\d{1,8}\.png', '1.png') is not None)
# print(re.match('\d{1,8}\.png', '11.png') is not None)
# w\d{1,8}(\.\d{1,8}|)
# print(re.search('w\d{1,8}(\.\d{1,8}|)', '11.png-w0.2-').span())

# line = '11.png-w1.2-t(我不知道你再说什,,m.mbabcdefg)'
# searchObj = re.search( r'-t\(.*?(?=\))', line)
# print(searchObj)
# print(searchObj.group()[3:])

# exit()


# 当前位置
current_x = 40;
current_y = 40;


# 输出
def echo(str):
    print(str)

def comparePersonal(x,y):
    a = 0;
    b = 0;

    s = re.search( r'\d{1,8}\.png', x)
    if(s is not None):
        a = int(s.group()[:-4])

    s = re.search( r'\d{1,8}\.png', y)
    if(s is not None):
        b = int(s.group()[:-4])

    flag = -1
    if(a > b): flag = 1
    if(a == b and len(x) > len(y)): flag = 1

    return flag


# 排序
def sortByFineName(a):
    # a.sort(key= functools.cmp_to_key(comparePersonal))
    return sorted(a, key=functools.cmp_to_key(comparePersonal))


# 结束
def end(str):
    echo(str)
    exit()

# 点击
def move(x, y, duration=0.1):
    global current_x;
    global current_y;
    current_x = x
    current_y = y
    pyautogui.moveTo(x, y, duration=duration)


# 点击
def click(x, y, conf = {}):
    if('ctype' not in conf): conf['ctype'] = 'cl';

    clicks = 1
    button = 'left'

    if(conf['ctype'] == 'cl'): button = 'left'
    if(conf['ctype'] == 'cld'):
        button = 'left'
        clicks = 2
    if(conf['ctype'] == 'cr'): button = 'right'
    if(conf['ctype'] == 'cm'): button = 'middle'
    if(conf['ctype'] == 'cn'): clicks = 0

    # 移动到坐标
    move(x, y)

    # 点击
    if(clicks > 0): pyautogui.click(button=button, clicks=clicks, interval=0.25)



# 滑动
def scroll(value):
    pyautogui.scroll(value)

# 输入文字
def inputstr(x, y, str):
    click(x, y)
    pyautogui.typewrite(str)

# 根据图片点击
def clickByImage(image_path, conf = {}):
    if('wait' not in conf): conf['wait'] = 0.2;
    if('ignore' not in conf): conf['ignore'] = False;
    if('text' not in conf): conf['text'] = '';
    if('roll' not in conf): conf['roll'] = 0;

    lo =  pyautogui.locateCenterOnScreen(image_path, confidence=0.8)
    if lo is None:
        msg = 'error:not find image in screen.' + image_path
        if(conf['ignore']): 
            echo('[ignore]'+msg)
            # 当前位置再点击一次
            click(current_x, current_y)
        else: end(msg)
    else:
        # 分辨率不同,除以二即可
        click(lo.x/2, lo.y/2, conf)

        # 输入文字
        if(conf['text'] != ''): inputstr(lo.x/2, lo.y/2, conf['text'])

        #滚动
        if(conf['roll'] != 0): scroll(conf['roll'])

        # 等待
        time.sleep(conf['wait'])


def flow(folder_path):
    # 文件后缀参数说明
    # -cl - click left 点击左键(默认)
    # -cld - click left double 双击左键
    # -cr - click right 点击右键
    # -cm - click middle 点击中键
    # -cn - click null 不点击

    # -ru10 - roll up 向上滑动10
    # -rd10 - roll down 向下滑动10

    # -w0.2 - wait 操作后等待0.2s

    # -t(this is text) - input some text 输入文字

    # -ignore - ignore 忽略找不到的情况

    # example1:'1.png' 点击左键
    # example2:'1.png-ignore' 点击左键,如果没有不报错
    # example3:'1.png-cl-w0.2' 点击左键,等待0.2s
    # example4:'1.png-cn-ru10' 不点击,向上滑动10
    # example5:'1.png-cl-ru10-t(this is text)' 点击左键,输入文字“this is text”
    echo('flow:' + folder_path)
    flow_files = []
    for root, dirs, files in os.walk(folder_path):
        for f in files:
            if(re.match('\d{1,8}\.png', f) is not None):
                flow_files.append(f)

    if(len(flow_files) == 0):
        end('error:no files in folder.' + folder_path)

    flow_files = sortByFineName(flow_files)



    for f in flow_files:
        conf = {}
        image_path = folder_path + '/' + f

        # 点击类型
        conf['ctype'] = 'cl'
        s = re.search( r'-c[a-z]{1,2}', f)
        if(s is not None):
            conf['ctype'] = s.group()[1:]



        # 等待时间
        conf['wait'] = 0.2
        s = re.search( r'-w\d{1,8}(\.\d{1,8}|)', f)
        if(s is not None):
            conf['wait'] = float(s.group()[2:])

        # 忽略
        conf['ignore'] = False
        s = re.search( r'-ignore', f)
        if(s is not None):
            conf['ignore'] = True


        # 输入文字
        conf['text'] = ''
        s = re.search( r'-t\(.*?(?=\))', f)
        if(s is not None):
            conf['text'] = s.group()[3:]


        # 滚动
        conf['roll'] = 0
        s = re.search( r'-ru\d{1,8}', f)
        if(s is not None):
            conf['roll'] = int(s.group()[3:])
        s = re.search( r'-rd\d{1,8}', f)
        if(s is not None):
            conf['roll'] = 0 - int(s.group()[3:])


        clickByImage(image_path, conf)



def init():
    path = current_folder + '/init'
    if(os.path.exists(path)):
        flow(path)


if __name__ == '__main__':

    options = sys.argv
    current_file_dir = os.path.dirname(__file__)


    if len(options) == 1:
        end('error:pleace input folder name.')

    current_xcx_name = options[1]
    current_folder = current_file_dir + '/' + current_xcx_name
    echo('current folder is:' + current_folder)


    # 判断子文件夹是否存在
    for index,item in enumerate(options):
        if(index > 1):
            path = current_folder + '/' + item
            if(not(os.path.exists(path))):
                end('error:folder not exist.' + path)


    pyautogui.PAUSE = 0.5 # 每个函数执行后停顿0.5秒
    pyautogui.FAILSAFE = True # 鼠标移到左上角会触发FailSafeException,因此快速移动鼠标到左上角也可以停止


    w, h = pyautogui.size()
    move(w/2, h/2) # 基本移动


    init()
    for root, dirs, files in os.walk(current_folder):
        for folder in dirs:
            if(folder != 'init' and ( len(options) == 2 or folder in options ) ):
                flow(current_folder + '/' + folder);