**
pyautogui自动化测试客户端程序
**
最近针对于 公司网盘的客户端产品,思考了一下,是否能够实现自动化测试呢?
嗯,谷歌一搜,果然没有让人失望,找到了这个:pyautogui
参考链接:让所有gui都自动化
- 首先需要安装pyautogui,很简单,cmd运行:
pip install pyautogui
- 然后打开你的python编译器,我的是pycharm,然后就可以写你想要自动化的程序了。
- 贴一张我写的简单的在QQ上发送消息的图和代码:
import pyautogui as pg #引入包
pg.FAILSAFE = True #设置保护措施
pg.PAUSE = 2.5 # 通过把pyautogui.PAUSE设置成float或int时间(秒),可以为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。在函数循环执行的时候,这样做可以让PyAutoGUI运行的慢一点,非常有用
secs_between_keys = 0.1 #设置输入时的时间间隔
num_seconds = 0.5 #控制鼠标移动速度
source_path = 'G:\\autoGUI\\autotest\\write.png' #截图路径(不知道做什么用的话,可参考前面贴的链接内容,很详细!)
x, y = pg.locateCenterOnScreen(source_path) #获取截图内容的坐标中心
pg.moveTo(x, y, duration=num_seconds) #鼠标移动到坐标处
pg.leftClick(x=x, y=y) #点击
pg.typewrite("wohainengzuogengduoshiqing", interval=secs_between_keys) #输入想输入的内容
pg.press(['space', 'enter']) #模拟键盘按下“enter”键,发送消息
需要注意的是:每次代码运行前,需要先在桌面打开要测试的软件界面。
熟练运用这个可以帮助我们做很多事情,当然,一般大多数的客户端程序的工作,它都可以完美自动化。
附上入门使用教程:
2015-08-17:输入中文bug没有解决,目前的解决方案是Python 2.X环境下安装pyperclip和pyautogui,用复制粘贴来实现。
In [ ]:
import pyperclip
import pyautogui
PyAutoGUI中文输入需要用粘贴实现 # Python 2版本的pyperclip提供中文复制
def paste(foo):
pyperclip.copy(foo)
pyautogui.hotkey('ctrl', 'v')
foo = u'学而时习之'
#移动到文本框
pyautogui.click(130,30)
paste(foo)
1.简介
1.1 目的
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码。
下面的代码让鼠标移到屏幕中央。
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
PyAutoGUI可以模拟鼠标的移动、点击、拖拽,键盘按键输入、按住操作,以及鼠标+键盘的热键同时按住等操作,可以说手能动的都可以。
1.2 例子
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
#鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
#用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
use tweening/easing function to move mouse over 2 seconds
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
#在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25)
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')
In [ ]:
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration=0.5) # 向右
distance -= 5
pyautogui.dragRel(0, distance, duration=0.5) # 向下
pyautogui.draIn gRel(-distance, 0, duration=0.5) # 向左
distance -= 5
pyautogui.dragRel(0, -distance, duration=0.5) # 向上
**1.4 保护措施(Fail-Safes)**
就像《魔法师的学徒》(Sorcerer’s Apprentice)会担水的扫帚,可以担水,却无力阻止水漫浴室。你的程序也可能会失控(即使是按照你的意思执行的),那时就需要中断。如果鼠标还在自动操作,就很难在程序窗口关闭它。
为了能够及时中断,PyAutoGUI提供了一个保护措施。当pyautogui.FAILSAFE = True时,如果把鼠标光标在屏幕左上角,PyAutoGUI函数就会产生pyautogui.FailSafeException异常。如果失控了,需要中断PyAutoGUI函数,就把鼠标光标在屏幕左上角。要禁用这个特性,就把FAILSAFE设置成False:
In [ ]:
import pyautogui
pyautogui.FAILSAFE = False
通过把pyautogui.PAUSE设置成float或int时间(秒),可以为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。在函数循环执行的时候,这样做可以让PyAutoGUI运行的慢一点,非常有用。例如:
In [ ]:
import pyautogui
pyautogui.PAUSE = 2.5
pyautogui.moveTo(100,100); pyautogui.click()
所有的PyAutoGUI函数在延迟完成前都处于阻塞状态(block)。(未来计划增加一个可选的非阻塞模式来调用函数。)
建议PAUSE和FAILSAFE一起使用。
**
2 安装与依赖
**
PyAutoGUI支持Python 2.x和Python 3.x
Windows:PyAutoGUI没有任何依赖,因为它用Python的ctypes模块所以不需要pywin32
pip3 install pyautogui
OS X:PyAutoGUI需要PyObjC运行AppKit和Quartz模块。这个模块在PyPI上的按住顺序是pyobjc-core和pyobjc
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc
sudo pip3 install pyautogui
Linux:PyAutoGUI需要python-xlib(Python 2)、python3-Xlib(Python 3)
sudo pip3 install python3-xlib
sudo apt-get scrot
sudo apt-get install python-tk
sudo apt-get install python3-dev
sudo pip3 install pyautogui