What is pywinauto¶
© Mark Mc Mahon and Contributors, 2006-2016
Released under the BSD 3-clause license
What is it?¶
pywinauto is a set of python modules to automate the Microsoft Windows GUI. At it’s simplest it allows you to send mouse and keyboard actions to windows dialogs and controls.
- Just run
pip install pywinauto
- Install the following Python packages
- Download latest pywinauto from https://github.com/pywinauto/pywinauto/releases
- Unpack and run
python setup.py install
To check you have it installed correctly Run Python
>>> from pywinauto.application import Application >>> app = Application(backend="uia").start("notepad.exe") >>> app.UntitledNotepad.type_keys("%FX")
How does it work¶
A lot is done through attribute access (
__getattribute__) for each class. For example
when you get the attribute of an Application or Dialog object it looks for a
dialog or control (respectively).
myapp.Notepad # looks for a Window/Dialog of your app that has a title 'similar' # to "Notepad" myapp.PageSetup.OK # looks first for a dialog with a title like "PageSetup" # then it looks for a control on that dialog with a title # like "OK"
This attribute resolution is delayed (with a default timeout) until it succeeds. So for example if you select a menu option and then look for the resulting dialog e.g.
app.UntitledNotepad.menu_select("File->SaveAs") app.SaveAs.ComboBox5.select("UTF-8") app.SaveAs.edit1.set_text("Example-utf8.txt") app.SaveAs.Save.click()
At the 2nd line the SaveAs dialog might not be open by the time this line is executed. So what happens is that we wait until we have a control to resolve before resolving the dialog. At that point if we can’t find a SaveAs dialog with a ComboBox5 control then we wait a very short period of time and try again, this is repeated up to a maximum time (currently 5 seconds!)
This is to avoid having to use time.sleep or a “wait” function explicitly.
If your application performs long time operation, new dialog can appear or disappear later. You can wait for its new state like so
app.Open.Open.click() # opening large file app.Open.wait_not('visible') # make sure "Open" dialog became invisible # wait for up to 30 seconds until data.txt is loaded app.window(title='data.txt - Notepad').wait('ready', timeout=30)
Some similar tools for comparison¶
- Python tools
- Other scripting language tools
- Other free tools
- (C#) Winium.Desktop - a young but good MS UI Automation based tool.
- (C#) TestStack.White - another good MS UI Automation based library with a long history.
- AutoIt - free tool with its own Basic-like language (Win32 API based, no .NET plans)
- AutoHotKey - native C++ tool with its own scripting language (.ahk)
- “Awesome test automation” list on GitHub
- A big list of open source tools for functional testing
- Commercial tools
Why write yet another automation tool if there are so many out there?¶
There are loads of reasons :-)
- Takes a different approach:
Most other tools are not object oriented you end up writing stuff like:
window = findwindow(title = "Untitled - Notepad", class = "Notepad") SendKeys(window, "%OF") # Format -> Font fontdialog = findwindow("title = "Font") buttonClick(fontdialog, "OK")
I was hoping to create something more userfriendly (and pythonic). For example the translation of above would be:
win = app.UntitledNotepad win.menu_select("Format->Font") app.Font.OK.click()
- Python makes it easy:
- Python is a great programming language, but there are no automation tools that were Pythonic (the very few libraries were implemented in Python).
- Localization as a main requirement:
“I work in the localization industry and GUI automation is used extensively as often all you need to do is ensure that your UI behaves and is correct with respect to the Source UI. This is actually an easier job then for testing the original source UI.
But most automation tools are based off of coordinates or text of the controls and these can change in the localized software. So my goal ( though not yet implemented) is to allow scripts to run unchanged between original source language (often English) and the translated software (Japanese, German, etc).”