What is pywinauto

(c) Mark Mc Mahon 2006-2009 Released under the LGPL licence

Full table of contents.

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.

Installation

To check you have it installed correctly Run Python

>>> from pywinauto import application
>>> app = application.Application.start("notepad.exe")
>>> app.notepad.TypeKeys("%FX")

Where to start

Look at the examples provided in test_application.py There are examples in there to work with Notepad and MSPaint.

How does it work

A lot is done through attribute access (__getattr__) 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 (currently a hard coded amount of time) until it succeeds. So for example if you Select a menu option and then look for the resulting dialog e.g.

app.Notepad.MenuSelect("File->SaveAs")
app.SaveAs.ComboBox5.Select("UTF-8")
app.SaveAs.edit1.SetText("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 1 second!)

This avoid the user having to use time.sleep or a “WaitForDialog” function.

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.MenuSelect("Format->Font")
app.Font.OK.Click()
Python makes it easy:
Python is a great programming language, but I didn’t find any automation tools that were Pythonic (I only found one that was implemented in python) and I didn’t care for it too much.
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).