【原创】基于pyautogui进行自动化测试
2018-06-18 03:30:39来源:未知 阅读 ()
前期准备:
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 基于tornado---异步并发接口 2019-07-24
- 华为Python面试题(原创) 2019-07-24
- Python 重点知识整理(基于Python学习手册第四版) 2019-07-24
- 【Demo 1】基于object_detection API的行人检测 1:环境与依 2019-07-24
- python 之 网络编程(基于UDP协议的套接字通信) 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash