【原创】基于pyautogui进行自动化测试

2018-06-18 03:30:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前期准备:

python3.6

pyautogui

pywinauto

以下代码实现内容:

1.打开记事本

2.记事本中输入This is a test

3.保存内容

4.退出进程

import pyautogui
from pywinauto import application
import time
import os

class Autogui():
    def __init__(self):
        self.app=application.Application()

    def run(self, tool_name):
        self.app.start(tool_name)
        time.sleep(2)

    #defined coordinates
    def coordinate(self,path):
        try:
            coordinates = pyautogui.locateOnScreen(path)
            x, y = pyautogui.center(coordinates)
            return x,y
        except Exception as e:
            print('Undefined coordinates: ', e)

    #Abnormal judgment
    def onscreen(self,x,y,content1):
        while pyautogui.onScreen(x, y) == True:
            print(content1)
            break

    def moveto(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)

    def moverel(self,x,y):
        pyautogui.moveRel(x,y,1,pyautogui.easeInQuad)

    def leftclick(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.click(button='left')

    def leftclick_down(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.mouseDown(button='left')

    def leftclick_up(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.mouseUp(button='left')

    def rightclick(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.click(button='right')

    def middleclick(self0,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.click(button='middle')

    def doubleclick(self,x,y):
        pyautogui.moveTo(x,y,0.5,pyautogui.easeInQuad)
        pyautogui.PAUSE=0.5
        pyautogui.doubleClick(button='left')

    def keydown(self,key):
        pyautogui.keyDown(key)

    def press(self,key):
        pyautogui.press(key)

    def keyup(self,key):
        pyautogui.keyUp(key)

    def hotkey(self,key1,key2):
        pyautogui.hotkey(key1,key2)

if __name__=='__main__':
    Test=Autogui()
    application_path=input('Application path : ')
    Test.run(application_path)
    # Test.run('notepad.exe')
    time.sleep(1)
    source_Path='./fouce.png'
    source_judge_Path='./fouce_judge.png'
    content1='The edit operation is correct'
    content2='The edit operation is fail'
    key1='s'
    key2='ctrlleft'

    # edit
    try:
        Test.leftclick(Test.coordinate(source_Path)[0],
                       Test.coordinate(source_Path)[1])
        pyautogui.typewrite('This is a test',0.1)
        time.sleep(0.5)
        Test.onscreen(Test.coordinate(source_judge_Path)[0],
                      Test.coordinate(source_judge_Path)[1], content1)
    except Exception as e:
        print(content2 + ': ', e)

    # save
    try:
        time.sleep(2)
        Test.hotkey(key2, key1)
        time.sleep(0.5)
        print('The save operation is correct')
    except Exception as e:
        print('Save Error: ', e)

    try:
        time.sleep(2)
        os.system('taskkill /F /IM notepad.exe')
    except Exception as e:
        print('Exit Error: ',e)
input()

 

贴上代码中两张图片,fouce.png  用来将光标定位到输入区域,fouce_judge.png 用来跟实际结果做对比,判断实际结果是否正确

fouce.png            

fouce_judge.png

 

pyautogui自动化脚本优点

1.不用担心因为分辨率的改变导致脚本失效

2.完全自定义日志输出

3.真UI自动化,可以判断每一步UI是否正确

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:第二章、python初识以及变量

下一篇:2.Python学习笔记:[输出三个数中最大值;while循环及break语句;co