自动化回放及对比录制图片像素,将失败截图存到文件夹中

import os                                                   
import json                                                 
import time                                                 
import pynput                                               
import math
import tkinter as tk
import operator
import datetime
import threading
from functools import reduce
import xlwt
import xlrd
from xlutils.copy import copy
from PIL import Image, ImageGrab, ImageTk


Threshold = 0                                               # 相似度阈值
renew = 10                                                  # 工作总结更新笔数
interval = 5                                                # 两笔交易间隔时间
Correctnumber = 0                                           # 正常执行次数
Wrongnumber = 0                                             # 异常执行次数
Summarize = ''                                              # 出错的笔数
number = 0                                                  # 总笔数
words1 = [
        {'名称': '\n', '数据': '\n'}]


def writenew():                                             # 创建日志
    global now
    now = Extractiontime()                                  # 获取当前时间
    with open("../../../工作日志/" + now + ".log", "w") as f:
        f.write("开始\n")
    f.close()


def writeAppend(n):                                         # 追加日志
    now = Extractiontime()                                  # 获取当前时间
    with open("../../../工作日志/" + now + ".log", "a") as f:
        f.write(n)
    f.close()
 

def write_to_excel(words, filename, sheet_name = '组别1'):    # 写入数据
    try:
        work_book = xlwt.Workbook(encoding='utf-8')         # 创建工作薄    
        sheet = work_book.add_sheet(sheet_name)             # 创建sheet表单
        head = []
        for k in words[0].keys():
            head.append(k)
        for i in range(len(head)):
            sheet.write(0, i, head[i])
        i = 1                                               # 添加内容
        for item in words:
            for j in range(len(head)):
                sheet.write(i, j, item[head[j]])
            i += 1
        work_book.save(filename)                            # 保存
        print('写入excel成功!')
    except Exception as e:
        print('写入excel失败!', e)


def append_to_excel(words, filename):                       # 追加数据
    try:
        word_book = xlrd.open_workbook(filename)            # 打开
        sheets = word_book.sheet_names()                    # 获取所有的sheet表单
        work_sheet = word_book.sheet_by_name(sheets[0])     # 获取第一个表单
        old_rows = work_sheet.nrows                         # 获取已经写入的行数 
        heads = work_sheet.row_values(0)                    # 获取表头信息
        new_work_book = copy(word_book)                     # 将xlrd对象变成xlwt
        new_sheet = new_work_book.get_sheet(0)              # 添加内容
        i = old_rows
        for item in words:
            for j in range(len(heads)):
                new_sheet.write(i, j, item[heads[j]])
            i += 1
        new_work_book.save(filename)
        print('追加成功!')
    except Exception as e:
        print('追加失败!', e)


def Extractiontime():                                       # 获取当前时间
    now = datetime.datetime.now()
    time1 = now.strftime("%Y-%m-%d %H:%M:%S")
    return time1
    

def get_image(file_nam, width, height):                     # 读取背景图
    im = Image.open(file_nam).resize((width, height))
    return ImageTk.PhotoImage(im)


def unicode_convert(input_data):                            # 转码
    if isinstance(input_data, dict):                        # 将unicode转换成str
        return {unicode_convert(key): unicode_convert(value) for key, value in input_data.iteritems()}
    elif isinstance(input_data, list):
        return [unicode_convert(element) for element in input_data]
    else:
        return input_data


def image_contrast(img1, img2):                             # 图片对比
  image1 = Image.open(img1)
  image2 = Image.open(img2)
  h1 = image1.histogram()
  h2 = image2.histogram()
  result = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a - b)**2, h1, h2))) / len(h1) )
  result = int (result)
  return result


def Errorscreenshot(q):                                     # 失败案例图片
    bbox = (0, 0, 1600, 900)
    im = ImageGrab.grab(bbox)
    im.save('../../../失败事例/' + q + '.png')          # 参数 保存截图文件的路径


def Screenshot():                                           # 图片截取
        with open('../坐标.txt', 'r') as u:
            m = u.read()
        z = m.split("(")
        v = z[1].split(")")
        num = v[0].split(",")
        n = num
        for i in range(0, 4, 1):
               n[i] = float(num[i])
        bbox = (n)
        im = ImageGrab.grab(bbox)
        im.save('../实际.png')                              # 保存截图文件的路径
        u.close()
        

def ExecuteCommandsFile():                                  # 据文件进行鼠标操作
     writenew()
     win.wm_attributes('-topmost', 0)
     while(1):
        with open("../录制记录.json") as f:
            command_list = json.loads(f.read())             # 将记录的命令写入命令列表
        time.sleep(interval)
        command_list = unicode_convert(command_list)
        mouse = pynput.mouse.Controller()                   # 创建鼠标和键盘的执行器,用于模拟键盘和鼠标的操作
        keyboard = pynput.keyboard.Controller()
        buttons = {                                         # 鼠标的两个按钮
            "Button.left": pynput.mouse.Button.left,
            "Button.right": pynput.mouse.Button.right
        }
        sTime = 0
        for command in command_list:                        # 执行每一条记录
            if command[0] == "click":
                mouse.position = (command[1][0], command[1][1])
                time.sleep(0.1)
                mouse.click(buttons[command[1][2]])
                h1 = Extractiontime()
                Combine = command[0] + ',' + str(command[1][0]) + ',' + str(command[1][1]) + ',' + command[1][2] + ',' + h1
                writeAppend(Combine)
                writeAppend('\n')
            elif command[0] == "press":
                if command[1][0][:3] == "Key":
                    keyboard.press(eval(command[1][0], {}, {
                        "Key": pynput.keyboard.Key
                    }))
                    h1 = Extractiontime()
                    Combine = command[0] + ',' + str(command[1]) + ',' + h1
                    writeAppend(Combine)
                    writeAppend('\n')
                else:
                    if "<255>" == command[1][0]:
                        continue
                    keyboard.press(command[1][0])
                    h1 = Extractiontime()
                    Combine = command[0] + ',' + str(command[1]) + ',' + h1
                    writeAppend(Combine)
                    writeAppend('\n')
            elif command[0] == "release":
                if command[1][0][:3] == "Key":
                    keyboard.release(eval(command[1][0], {}, {
                        "Key": pynput.keyboard.Key
                    }))
                    h1 = Extractiontime()
                    Combine = command[0] + ',' + str(command[1]) + ',' + h1
                    writeAppend(Combine)
                    writeAppend('\n')
                else:
                    if "<255>" == command[1][0]:
                        continue
                    keyboard.release(command[1][0])
                    h1 = Extractiontime()
                    Combine = command[0] + ',' + str(command[1]) +  ',' + h1
                    writeAppend(Combine)
                    writeAppend('\n')
            time.sleep(command[2] - sTime)                  # command[2]代表此操作距离开始操作所经过的时间,用它减去已经经过的时间就是距离下一次操作的时间 
            sTime = command[2]                              # 更新时间
        time.sleep(3)
        Screenshot()                                        # 图片截取global num
        img1 = '../录制截图.png'                            # 指定图片路径 录制保存的图片
        img2 = '../实际.png'
        result = image_contrast(img1, img2)                 # 图片对比global num
        print("相似度为:")                                  # 越小越像
        print(result)
        Combine = "相似度为:" + str(result)
        writeAppend(Combine)
        global Correctnumber
        global Wrongnumber
        global Summarize
        global number   
        if result > Threshold:
            print("自动化异常!")
            sj = Extractiontime()
            Errorscreenshot(sj)
            Wrongnumber = Wrongnumber + 1
            print("正常执行笔数:", Correctnumber)
            print("异常执行笔数:", Wrongnumber)
            number = Correctnumber + Wrongnumber
            Summarize = Summarize + str(number)
            Summarize = Summarize + ';'
            writeAppend('\n自动化异常!\n')
        else:
            print("自动化正常!")
            Correctnumber = Correctnumber + 1
            print("正常执行笔数:", Correctnumber)
            print("异常执行笔数:", Wrongnumber)
            number = Correctnumber + Wrongnumber
            writeAppend('\n自动化正常!\n')            
        Combine = "正常执行笔数:" + str(Correctnumber) + '\n'
        writeAppend(Combine)
        Combine = "异常执行笔数:" + str(Wrongnumber) + '\n'
        writeAppend(Combine)
        Combine = "分别在第几笔执行异常:" + Summarize + '\n'
        writeAppend(Combine)
        Combine = "总笔数:" + str(number) + '\n'
        writeAppend(Combine)
        writeAppend('\n')
        writeAppend('\n')
        print("\n")
        if number % renew == 0:
            words2 = [
                    {'名称': '正常执行笔数:', '数据': Correctnumber},
                    {'名称': '异常执行笔数:', '数据': Wrongnumber},
                    {'名称': '分别在第几笔执行异常:', '数据': Summarize},
                    {'名称': '总笔数:', '数据': number},
                    {'名称': '\n', '数据': '\n'},
                    {'名称': '\n', '数据': '\n'}
                ]
            append_to_excel(words = words2, filename = '../../../工作总结.xls')
        f.close()


def Finish():                                               # 终结程序
    if number > 0:
        words2 = [
                    {'名称': '正常执行笔数:', '数据': Correctnumber},
                    {'名称': '异常执行笔数:', '数据': Wrongnumber},
                    {'名称': '分别在第几笔执行异常:', '数据': Summarize},
                    {'名称': '总笔数:', '数据': number},
                    {'名称': '\n', '数据': '\n'},
                    {'名称': '\n', '数据': '\n'}
                    ]
        append_to_excel(words = words2, filename = '../../../工作总结.xls')
    os._exit(0)
    

def getTextInput():                                         # 获取文本框输入
    global Threshold
    global renew
    global interval
    Threshold = textExample.get("1.0", "end")               # 获取文本输入框的内容
    renew = textExample1.get("1.0", "end")
    interval = textExample2.get("1.0", "end")
    Threshold = int(Threshold)
    renew = int(renew)
    interval = int(interval)
    print(Threshold)                                        # 输出结果
    print(renew)
    print(interval)
    

def thread_it(func, *args):                                 # 线程
    t = threading.Thread(target = func, args = args)        # 创建 
    t.setDaemon(True)                                       # 守护 
    t.start()                                               # 启动
    

write_to_excel(words = words1, filename = '../../../工作总结.xls', )   
win = tk.Tk()
win.wm_attributes('-topmost', 1)                        # 置顶页面
win.title('应用执行框')                                 # 设置窗口title和大小
win.geometry('200x200')
win.resizable(0,0)                                      # 防止用户调整尺寸
canvas = tk.Canvas(win, height = 500, width = 500)      # 画布  设置背景图片
im_root = get_image('../回放背景图.png', width = 500, height = 500)
canvas.create_image(250, 250, image = im_root)
canvas.pack()
onebutton = tk.Button(win, bg = 'Light blue', text = "开始", width = 5, height = 2, command = lambda :thread_it(ExecuteCommandsFile))# 创建按钮
twobutton = tk.Button(win, bg = 'Light blue', text = "关闭", width = 5, height = 2, command = lambda :thread_it(Finish))
threebutton = tk.Button(win, bg = 'Light blue', text = "确定", height = 2, width = 5, command = getTextInput)
textExample = tk.Text(win, height = 1, width = 4)       # 创建文本输入框
textExample1 = tk.Text(win, height = 1, width = 4)
textExample2 = tk.Text(win, height = 1, width = 4)
textExample.pack()                                      # 把Text放在win上面,显示Text这个控件
textExample1.pack()
textExample2.pack()
L1 = tk.Label(win, text = "相似度阈值设置:", font = ("SimHei", 12))# 输出样式及内容
L2 = tk.Label(win, text = "工作总结更新设置:", font = ("SimHei", 12))
L3 = tk.Label(win, text = "交易间隔时间设置:", font = ("SimHei", 12))
L1.place(x = 5, y = 65)                                 # 输出样式及内容
L2.place(x = 5, y = 10)
L3.place(x = 5, y =115)
onebutton.place(x = 5, y = 150)
twobutton.place(x = 150, y = 150)
threebutton.place(x = 75, y = 150)
textExample.place(x = 160, y = 67)
textExample1.place(x = 160, y = 12)
textExample2.place(x = 160, y = 117)
win.mainloop()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值