[TOC]
# 什么是pywinauto
? Mark Mc Mahon and [Contributors](https://github.com/pywinauto/pywinauto/graphs/contributors), 2006-2018
Released under the BSD 3-clause license
## 它是什么
pywinauto是一組用于自動化Microsoft Windows GUI的python模塊。 最簡單的是,它允許您將鼠標和鍵盤操作發送到窗口對話框和控件。
## 安裝
* 運行 `pip install pywinauto`
## 手動安裝
* 安裝以下Python包
* [pyWin32](http://sourceforge.net/projects/pywin32/files/pywin32/Build%20220/)
* [comtypes](https://github.com/enthought/comtypes/releases)
* [six](https://pypi.python.org/pypi/six)
* *(可選)* [Pillow](https://pypi.python.org/pypi/Pillow/2.7.0) (截圖用)
* 從[https://github.com/pywinauto/pywinauto/releases](https://github.com/pywinauto/pywinauto/releases)中下載最新的pywinauto
* 解壓并運行 `python setup.py install`
要檢查是否已正確安裝,請運行Python,**中文環境可能不適用**
~~~
>>> from pywinauto.application import Application
>>> app = Application(backend="uia").start("notepad.exe")
>>> app.UntitledNotepad.type_keys("%FX")
~~~
## 它是如何工作的
核心概念在 [入門指南](getting_started.md)中描述.
通過每個類的屬性訪問(`__getattribute__`)完成了很多工作。 例如,當您獲取Application或Dialog對象的屬性時,它會分別查找對話框或控件。
~~~
myapp.Notepad # 查找應用程序的窗口/對話框,其標題為'similar'
# to "Notepad"
myapp.PageSetup.OK # 首先查找標題為“PageSetup”的對話框
# 然后它在帶有標題的對話框中查找控件
# 比如 "OK"
~~~
此屬性解析會延遲(使用默認超時),直到成功為止。 因此,例如,如果您選擇菜單選項,然后查找結果對話框,例如
~~~
app.UntitledNotepad.menu_select("File->SaveAs")
app.SaveAs.ComboBox5.select("UTF-8")
app.SaveAs.edit1.set_text("Example-utf8.txt")
app.SaveAs.Save.click()
~~~
在第2行,執行此行時可能無法打開SaveAs對話框。 所以會發生什么,我們要等到我們有一個控件要解決之前解決對話框。 此時,如果我們找不到帶有ComboBox5控件的SaveAs對話框,那么我們會等待很短的時間再試一次,這會重復到最大時間(當前為5秒!)
這是為了避免必須明確使用```time.sleep```或```wait```函數。
如果您的應用程序執行長時間操作,則新對話框可能會在以后顯示或消失。 你可以等待它的新狀態
~~~
app.Open.Open.click() # 打開大文件
app.Open.wait_not('visible') # 確保“打開”對話框變得不可見
# 等待最多30秒,直到加載data.txt
app.window(title='data.txt - Notepad').wait('ready', timeout=30)
~~~
## 一些類似的比較工具
* Python tools
* [PyAutoGui](https://github.com/asweigart/pyautogui) - 一個流行的跨平臺庫(具有基于圖像的搜索,沒有基于文本的控件操作)。
* [Lackey](https://github.com/glitchassassin/lackey) - Sikuli的純Python替代品(基于圖像模式匹配)。
* [AXUI](https://github.com/xcgspring/AXUI) - MS UI Automation API的一個包裝器。
* [winGuiAuto](https://github.com/arkottke/winguiauto) - 另一個使用Win32 API的模塊。
* 其他腳本語言工具
* (Perl) [Win32::GuiTest](http://winguitest.sourceforge.net/)
* (Ruby) [Win32-Autogui](https://github.com/robertwahler/win32-autogui) - Win32 API的包裝器。
* (Ruby) [RAutomation](https://github.com/jarmo/RAutomation) - 有3個適配器:Win32 API,UIA,AutoIt。
* 其他免費工具
* (C#) [Winium.Desktop](https://github.com/2gis/Winium.Desktop) - 一個年輕但很好的基于MS UI Automation的工具。
* (C#) [TestStack.White](https://github.com/TestStack/White) - 另一個很好的基于MS UI Automation的庫,歷史悠久。
* [AutoIt](http://www.autoitscript.com/) - 具有自己的類似Basic語言的免費工具(基于Win32 API,沒有.NET計劃)
* [AutoHotKey](https://github.com/Lexikos/AutoHotkey_L/) - 具有自己的腳本語言的原生C ++工具(.ahk)
* [“很棒的測試自動化” 列表](https://github.com/atinfo/awesome-test-automation) on GitHub
* [用于功能測試的大量開源工具](http://www.opensourcetesting.org/category/functional/)
* 商業工具
* WinRunner ([http://www.mercury.com/us/products/quality-center/functional-testing/winrunner/](http://www.mercury.com/us/products/quality-center/functional-testing/winrunner/))
* SilkTest ([http://www.segue.com/products/functional-regressional-testing/silktest.asp](http://www.segue.com/products/functional-regressional-testing/silktest.asp))
* Many Others ([http://www.testingfaqs.org/t-gui.html](http://www.testingfaqs.org/t-gui.html))
## 如果有那么多的話,為什么還要編寫另一個自動化工具?
這里有很多原因:-)
**采取不同的方法:**
大多數其他工具不是面向對象的,你最終寫的東西如下:
~~~
window = findwindow(title = "Untitled - Notepad", class = "Notepad")
SendKeys(window, "%OF") # Format -> Font
fontdialog = findwindow("title = "Font")
buttonClick(fontdialog, "OK")
~~~
我希望創造一些更加用戶友好(和pythonic)的東西。 例如,上面的編程轉換如下:
~~~
win = app.UntitledNotepad
win.menu_select("Format->Font")
app.Font.OK.click()
~~~
**Python讓它變得簡單:**
Python是一種很棒的編程語言,但是沒有自動化工具是Pythonic(很少有庫是用Python實現的)。
**本地化是主要要求:**
Mark:
“我在本地化行業工作,GUI自動化被廣泛使用,因為您需要做的就是確保您的UI在Source UI方面的行為和正確性。 實際上,這對于測試原始源UI來說實際上更容易。
但是大多數自動化工具都基于控件的坐標或文本,這些可以在本地化軟件中更改。 所以我的目標(雖然尚未實現)是允許腳本在原始源語言(通常是英語)和翻譯軟件(日語,德語等)之間保持不變。”
- 什么是Pywinauto
- 入門指南
- 如何
- 等待長時間操作
- 遠程執行指南
- 每種不同控制類型可用的方法
- 貢獻者
- 開發筆記
- 待辦項目
- 更新日志
- 基本用戶輸入模塊
- pywinauto.mouse
- pywinauto.keyboard
- 主要用戶模塊
- pywinauto.application
- pywinauto.findbestmatch
- pywinauto.findwindows
- pywinauto.timings
- 特定功能
- pywinauto.clipboard
- pywinauto.win32_hooks
- 控件參考
- pywinauto.base_wrapper
- pywinauto.controls.hwndwrapper
- pywinauto.controls.menuwrapper
- pywinauto.controls.common_controls
- pywinauto.controls.win32_controls
- pywinauto.controls.uiawrapper
- pywinauto.controls.uia_controls
- Pre-supplied Tests
- pywinauto.tests.allcontrols
- pywinauto.tests.asianhotkey
- pywinauto.tests.comboboxdroppedheight
- pywinauto.tests.comparetoreffont
- pywinauto.tests.leadtrailspaces
- pywinauto.tests.miscvalues
- pywinauto.tests.missalignment
- pywinauto.tests.missingextrastring
- pywinauto.tests.overlapping
- pywinauto.tests.repeatedhotkey
- pywinauto.tests.translation
- pywinauto.tests.truncation
- 后端內部實施模塊
- pywinauto.backend
- pywinauto.element_info
- pywinauto.win32_element_info
- pywinauto.uia_element_info
- pywinauto.uia_defines
- 內部模塊
- pywinauto.controlproperties
- pywinauto.handleprops
- pywinauto.xml_helpers
- pywinauto.fuzzydict
- pywinauto.actionlogger
- pywinauto.sysinfo
- pywinauto.remote_memory_block