Automated GUI Testing
by George NistoricaAugust 11, 2005
Why Perl?
Simply put: because you like Perl.
The long story is that there are all sorts of software packages that you may use to automate graphical applications. Are they really good fits for what you want to do?
Windows has many libraries that help you automate such things, but do the applications you use support those automation libraries? Too many do not. Moreover, is this enough for you to say you have tested a certain GUI feature? If not, read on.
What You Need
You need a working installation of Perl, with Perl/Tk included. I recommend ActiveState's ActivePerl. You also need the Win32::GuiTest module. Install it from the CPAN or, ideally, through PPM.
Example Code
|
Related Reading
Perl Testing: A Developer's Notebook |
Download the tester.pl and the tested.pl programs. They need to both be in the same directory. First run the tested.pl program in order to see the windows it has and how it looks. The program does nothing by itself; it just serves as a "run" application. tester.pl is more interesting. It spawns tested.pl and starts sending it input (mouse moves, mouse clicks, and keystrokes).
I tested these two programs on Windows 2000 Professional and Windows XP Home Edition using ActiveState's distribution of Perl.
The tested.pl program is just a dummy GUI used to demonstrate the examples. It uses Tk, so although it is a Win32 GUI, it isn't a native one. This has the effect that not all of the functions you can use with Win32::GuiTest will work as you would expect them to work against a native Win32 GUI. Fortunately, there are workarounds.
A Few Words About Windows
Graphical user interfaces manage windows. Windows are just reusable objects with which users can interact. Almost all GUIs have more than just one window. I use "window" just as a generic term for any graphical object that an application may produce. This means that "window" is an abstract term after all.
Windows have common elements that you need to consider before writing a program that interacts with a GUI.
- Each window belongs to a window class (making it possible to search them by class).
- Windows have an organizational hierarchy; every GUI has at least one root window, and every window may have child windows. Windows form a tree. This makes them searchable (by class or not) in depth: start from a root window and search among its siblings.
- Some windows have text attached to them. This is useful to identify windows.
- Windows have an numeric ID that uniquely identifies them.
This means that you can identify windows by any of their text, class, and parent window attributes. You can also pinpoint a window by its ID.
Finding Windows
When testing a GUI, first make sure the application you want to test has
started. To do this, use the Win32::GuiTest exported function named
FindWindowLike(). Remember that hierarchy of Windows? If you
search for an Edit window, you may find it in the wrong place. That
There can be multiple different GUIs started that have editor windows.
There should be a way to differentiate between these hypothetical editor windows--and the hierarchical organization of windows helps.
First look for the main window of the application, and then descend the hierarchy (that you have to know beforehand) until you reach the desired window.
How can you know the windows hierarchy? There are two main ways. If you have written the GUI yourself or have access to its sources and have enough experience, you may find out what the hierarchy of windows is. Unfortunately, that's quite tricky and prone to error.
Another much simpler way to do this on Windows platforms is to use the free WinSpy++ program. Basically, it allows you to peek at an application's window structure.
When you use WinSpy++ to look at an application windowing structure, you will notice that every window has a numeric handle, expressed in hex. However, Perl expresses in decimal. This will come up again in a moment.

