目錄
1、前言
2、簡介
3、安裝
4、常用函數(shù)
5、保護(hù)措施
6、鼠標(biāo)函數(shù)
7、鍵盤函數(shù)
8、消息彈窗函數(shù)
9、截屏函數(shù)
?10、示例
1、前言
在使用 Selenium 進(jìn)行自動化測試時(shí),鼠標(biāo)事件可以用 ActionChains 類,鍵盤事件可以用 Keys 類。本篇將介紹一款自動化工具-PyAutoGUI,除了可以滿足鼠標(biāo)、鍵盤事件操作外,還可以進(jìn)行消息彈窗、截屏等操作。
2、簡介
PyAutoGUI 是一個(gè)純 Python 的 GUI 自動化工具,其目的是可以用程序自動控制鼠標(biāo)和鍵盤操作,多平臺支持(Windows,macOS,Linux),并在 Python 2 和 3 上運(yùn)行。
PyAutoGUI 可以模擬鼠標(biāo)的移動、點(diǎn)擊、拖拽,鍵盤按鍵輸入、按住操作,以及鼠標(biāo)+鍵盤的熱鍵同時(shí)按住等操作,可以說手能動的都可以。
PyAutoGUI 有幾個(gè)特點(diǎn):
1、移動鼠標(biāo)并單擊其他應(yīng)用程序的窗口。
2、向應(yīng)用程序發(fā)送點(diǎn)擊輸入(例如,填寫表格)。
3、截取屏幕截圖,并給出一個(gè)圖像(例如,一個(gè)按鈕或復(fù)選框),然后在屏幕上找到它。
4、找到應(yīng)用程序的窗口,然后移動、調(diào)整大小、最大化、最小化或關(guān)閉它(目前僅限 Windows)。
5、顯示警報(bào)和消息框。
Github地址:
https://github.com/asweigart/pyautogui
3、安裝
1、Windows?環(huán)境下
PyAutoGUI 沒有任何依賴,因?yàn)樗?Python 的 ctypes 模塊,所以不需要 pywin32。
pip3 install pyautogui
2、macOS?環(huán)境下
PyAutoGUI 需要 PyObjC 運(yùn)行 AppKit 和 Quartz 模塊。
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc
sudo pip3 install pyautogui
3、Linux?環(huán)境下
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
4、常用函數(shù)
position():返回整數(shù)元組?(x, y),分別表示鼠標(biāo)光標(biāo)所在位置的 XY 軸坐標(biāo)。
size():返回顯示器的尺寸整數(shù)元組?(x, y)。
onScreen():要檢查 XY 坐標(biāo)是否在屏幕上,需要用此函數(shù)來檢驗(yàn),如果在屏幕上返回 True,否則返回 False。
1、獲取當(dāng)前鼠標(biāo)的坐標(biāo)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
print(pyautogui.position())
運(yùn)行結(jié)果:
2、獲取當(dāng)前屏幕的分辨率(寬度和高度)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
print(pyautogui.size())
運(yùn)行結(jié)果:
3、判斷坐標(biāo)是否在屏幕上
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
x, y =?500,?600
print(pyautogui.onScreen(x, y))
運(yùn)行結(jié)果:
5、保護(hù)措施
PAUSE?方法可以進(jìn)行增加延遲時(shí)間,例如 3.5 秒。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.PAUSE =?3.5
6、鼠標(biāo)函數(shù)
屏幕位置使用 X 和 Y 軸的笛卡爾坐標(biāo)系。
原點(diǎn)?(0,0)?在左上角,分別向右、向下增大。
如果屏幕像素是 1920*1080,那么右下角的坐標(biāo)是?(1919, 1079)。
moveTo():函數(shù)會把鼠標(biāo)光標(biāo)移動到指定的 XY 軸坐標(biāo)處。如果傳入 None 值,則表示使用當(dāng)前光標(biāo)的對象軸坐標(biāo)值。
moveRel():如果你想讓光標(biāo)以當(dāng)前位置為原點(diǎn),進(jìn)行相對移動,就用此函數(shù)。
dragTo()?和 dragRel():函數(shù)與 moveTo()?和 moveRel()?函數(shù)類似。另外,他們有一個(gè) button 參數(shù)可以設(shè)置成 left,middle 和 right 三個(gè)鍵。
click():函數(shù)模擬單擊鼠標(biāo)左鍵一次的行為。參數(shù)可以設(shè)置,其中,button 屬性可以設(shè)置成 left,middle 和 right。
doubleClick(),tripleClick()?和 rightClick():函數(shù)來實(shí)現(xiàn)雙擊、三擊和右擊操作。
mouseDown()?和 mouseUp():函數(shù)可以實(shí)現(xiàn)鼠標(biāo)按下和鼠標(biāo)松開的操作。兩者參數(shù)相同,有 x,y 和 button。
scroll():函數(shù)控制鼠標(biāo)滾輪的滾動,amount_to_scroll 參數(shù)表示滾動的格數(shù)。正數(shù)則頁面向上滾動,負(fù)數(shù)則向下滾動。
1、鼠標(biāo)移動
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
num_seconds =?1.2
# 用num_seconds秒的時(shí)間把光標(biāo)移動到(x, y)位置
pyautogui.moveTo(x, y, duration=num_seconds)
# 用num_seconds秒的時(shí)間把光標(biāo)的
# X軸(水平)坐標(biāo)移動xOffset,
# Y軸(豎直)坐標(biāo)向下移動yOffset。
xOffset, yOffset =?50,?100
pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)
2、鼠標(biāo)移動(緩動/漸變)-?改變光標(biāo)移動過程的速度和方向。通常鼠標(biāo)是勻速直線運(yùn)動的。
# 開始很慢,不斷加速
pyautogui.moveTo(100,?100,?2, pyautogui.easeInQuad)
# 開始很快,不斷減速
pyautogui.moveTo(100,?100,?2, pyautogui.easeOutQuad)
# 開始和結(jié)束都快,中間比較慢
pyautogui.moveTo(100,?100,?2, pyautogui.easeInOutQuad)
# 一步一徘徊前進(jìn)
pyautogui.moveTo(100,?100,?2, pyautogui.easeInBounce)
# 徘徊幅度更大,甚至超過起點(diǎn)和終點(diǎn)
pyautogui.moveTo(100,?100,?2, pyautogui.easeInElastic)
3、鼠標(biāo)拖拽
# 按住鼠標(biāo)左鍵,把鼠標(biāo)拖拽到(100, 200)位置
pyautogui.dragTo(100,?200, button='left')
# 按住鼠標(biāo)左鍵,用2秒鐘把鼠標(biāo)拖拽到(300, 400)位置
pyautogui.dragTo(300,?400,?2, button='left')
# 按住鼠標(biāo)右鍵,用2秒鐘把鼠標(biāo)拖拽到(30,0)位置
pyautogui.dragTo(30,?0,?2, button='right')
4、鼠標(biāo)點(diǎn)擊
# 先移動到(100, 200)再單擊
pyautogui.click(x=100, y=200, duration=2)
# 右鍵
pyautogui.click(button='right')
# 雙擊左鍵
pyautogui.click(clicks=2)
# 兩次單擊之間停留0.25秒
pyautogui.click(clicks=2, interval=0.25)
# 三擊右鍵
pyautogui.click(button='right', clicks=2, interval=0.25)
5、鼠標(biāo)按下和鼠標(biāo)松開
# 鼠標(biāo)左鍵按下再松開
pyautogui.mouseDown(); pyautogui.mouseUp()
# 按下鼠標(biāo)右鍵
pyautogui.mouseDown(button='right')
# 移動到(100, 200)位置,然后松開鼠標(biāo)右鍵
pyautogui.mouseUp(button='right', x=100, y=200)
6、鼠標(biāo)滾動
# 向上滾動10格
pyautogui.scroll(10)
# 向下滾動10格
pyautogui.scroll(-10)
# 移動到(100, 100)位置再向上滾動10格
pyautogui.scroll(10, x=100, y=100)
7、鍵盤函數(shù)
typewrite():函數(shù)可以實(shí)現(xiàn)字符輸入。要在兩次輸入間增加時(shí)間間隔,可以用 interval 參數(shù)。此函數(shù)只能用于單個(gè)字符鍵,不能按 SHITF 和 F1 這些功能鍵。
KEYBOARD_KEYS:獲取按鍵名稱。
press():函數(shù)把 pyautogui.KEYBOARD_KEYS 里面按鍵對應(yīng)的字符串輸入進(jìn)去。
hotkey():函數(shù)來綁定若干鍵盤熱鍵。像 Ctrl-S 或 Ctrl-Shift-1 都可以用此函數(shù)來實(shí)現(xiàn)。
1、鍵盤輸入
# 輸入Hello world!
pyautogui.typewrite('Hello world!')
# 每次輸入間隔0.25秒,輸入Hello world!
pyautogui.typewrite('Hello world!', interval=0.25)
# 多個(gè)鍵也可以
pyautogui.typewrite(['a',?'b',?'c',?'left',?'backspace',?'enter',?'f1'], interval=0.5)
2、按鍵名稱
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
print(pyautogui.KEYBOARD_KEYS)
運(yùn)行結(jié)果:
3、鍵盤按鍵
# ENTER鍵
pyautogui.press('enter')
# F1鍵
pyautogui.press('f1')
# 左方向鍵
pyautogui.press('left')
# 用數(shù)組把一組鍵傳入
pyautogui.press(['left',?'left',?'left'])
press()?函數(shù)其實(shí)是 keyDown()?和 keyUp()?函數(shù)的包裝,模擬的按下然后松開兩個(gè)動作。這兩個(gè)函數(shù)可以單獨(dú)調(diào)用。
例如,按下 shift 鍵的同時(shí)按3次左方向鍵。每個(gè)按鍵的按下和松開也可以單獨(dú)調(diào)用。
# 按下`shift`鍵
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松開`shift`鍵
pyautogui.keyUp('shift')
4、鍵盤熱鍵
pyautogui.hotkey('ctrl',?'a')?# 全選
pyautogui.hotkey('ctrl',?'c')?# 復(fù)制
pyautogui.hotkey('ctrl',?'v')?# 粘貼
8、消息彈窗函數(shù)
如果你需要消息彈窗,通過單擊 OK 暫停程序,或者向用戶顯示一些信息,消息彈窗函數(shù)就會有類似JavaScript 的功能。
alert():函數(shù)顯示一個(gè)簡單的帶文字和 OK 按鈕的消息彈窗。用戶點(diǎn)擊后返回 button 的文字。
confirm():函數(shù)顯示一個(gè)簡單的帶文字、OK 和 Cancel 按鈕的消息彈窗,用戶點(diǎn)擊后返回被點(diǎn)擊 button 的文字,支持自定義數(shù)字、文字的列表。
prompt():函數(shù)可以輸入的消息彈窗,帶 OK 和 Cancel 按鈕。用戶點(diǎn)擊 OK 按鈕返回輸入的文字,點(diǎn)擊 Cancel 按鈕返回 None。
password():函數(shù)樣式同 prompt(),用于輸入密碼,消息用?*?表示。帶 OK 和 Cancel 按鈕。用戶點(diǎn)擊 OK 按鈕返回輸入的文字,點(diǎn)擊 Cancel 按鈕返回 None。
1、alert()?函數(shù)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.alert(text='點(diǎn)擊OK按鈕', title='alert()函數(shù)', button='OK')
運(yùn)行結(jié)果:
2、confirm()?函數(shù)
(1)OK?和?Cancel?按鈕的消息彈窗
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.confirm(text='點(diǎn)擊OK或Cancel按鈕', title='confirm()函數(shù)', buttons=['OK',?'Cancel'])
運(yùn)行結(jié)果:
(2)10?個(gè)按鍵?0-9?的消息彈窗
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.confirm(text='點(diǎn)擊0到9按鈕', title='confirm()函數(shù)', buttons=range(10))
運(yùn)行結(jié)果:
3、prompt()?函數(shù)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.prompt(text='輸入消息彈窗', title='prompt()函數(shù)', default='用戶點(diǎn)擊OK')
運(yùn)行結(jié)果:
4、password()?函數(shù)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
pyautogui.password(text='輸入消息彈窗', title='password()函數(shù)', default='用戶點(diǎn)擊OK', mask='*')
運(yùn)行結(jié)果:
9、截屏函數(shù)
PyAutoGUI 用 Pillow/PIL 庫實(shí)現(xiàn)圖片相關(guān)的識別和操作。
在 Linux 里面,你必須執(zhí)行 sudo apt-get install scrot 來使用截屏特性。
1、截屏
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?pyautogui
# 返回一個(gè)Pillow/PIL的Image對象
pyautogui.screenshot()
pyautogui.screenshot('Desktop.png')
運(yùn)行結(jié)果:
如果你不需要截取整個(gè)屏幕,還有一個(gè)可選的 region 參數(shù)。你可以把截取區(qū)域的左上角 XY 坐標(biāo)值和寬度、高度傳入截取。
im = pyautogui.screenshot(region=(0,0,300,400))
2、如果你有一個(gè)圖片文件想在上面做點(diǎn)擊操作,你可以用 locateOnScreen()?函數(shù)來定位。
# 返回(最左x坐標(biāo),最頂y坐標(biāo),寬度,高度)
pyautogui.locateOnScreen('pyautogui/looks.png')
3、locateAllOnScreen()?函數(shù)會尋找所有相似圖片,返回一個(gè)生成器。
for?i?in?pyautogui.locateAllOnScreen('pyautogui/looks.png'):
????print(i)
list(pyautogui.locateAllOnScreen('pyautogui/looks.png'))
4、locateCenterOnScreen()?函數(shù)會返回圖片在屏幕上的中心 XY 軸坐標(biāo)值。
pyautogui.locateCenterOnScreen('pyautogui/looks.png')
?10、示例
使用 Selenium 打開 Chrome 瀏覽器,訪問百度首頁,輸入關(guān)鍵字進(jìn)行查詢,之后向下滾動后再進(jìn)行截屏的一系列操作。
由于 PyAutoGUI 不支持中文輸入,這里使用 pyperclip 庫,采用“復(fù)制粘貼”的方式來輸入中文。
注:由于作者是 macOS 環(huán)境下運(yùn)行,ctrl+v 粘貼則是 command+v。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試
import?time
import?pyperclip
import?pyautogui
from?selenium?import?webdriver
from?selenium.webdriver.common.by?import?By
class?TestMyTest():
????def?setup_method(self):
????????self.driver = webdriver.Chrome()
????????self.pyautogui = pyautogui
????????self.pyperclip = pyperclip
????def?teardown_method(self):
????????self.driver.quit()
????def?test_myTest(self):
????????self.driver.get("https://www.baidu.com/")
????????time.sleep(2)
????????self.pyautogui.typewrite('pyautogui', interval=0.25)
????????time.sleep(2)
????????self.pyperclip.copy("自動化測試")
????????time.sleep(2)
????????self.pyautogui.hotkey('command',?'v')
????????time.sleep(2)
????????self.driver.find_element(By.ID,?"su").click()
????????time.sleep(2)
????????self.pyautogui.scroll(-10)
????????time.sleep(2)
????????self.pyautogui.screenshot('Desktop.png')
精彩推薦
面試筆試系列
思維導(dǎo)圖系列
Linux常用命令壁紙
接口Requests系列
測試框架pytest系列
Jmeter快速上手之接口測試
自動化測試框架結(jié)構(gòu)圖
移動安全框架(MobSF)文章來源:http://www.zghlxwxcb.cn/news/detail-676257.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-676257.html
到了這里,關(guān)于讓所有GUI都自動化-PyAutoGUI(GUI自動化工具)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!