Wednesday, June 4, 2008

Exploring test application: Win32 API

Let's look on what we can discover about our simple test application when it is running. Before IronPython, we had two options:
  • Win32 API
  • Accessibility
Win32 API provides several functions that return information about an application. First, we have to find the appliction handle. We utilize functions EnumWindows and GetWindowText from Python for Windows extensions project:
def enum(hwnd, extra):
global app_handle
if winxpgui.GetWindowText(hwnd).find('GUIAT - Proof') >= 0:
app_handle = hwnd

app_handle = None
winxpgui.EnumWindows(enum, '')
print 'Test application handle:', app_handle
When we know the application handle, we can enumerate its children:
def enum_child(hwnd, extra):
print '%s %s: %s' % (hwnd, winxpgui.GetClassName(hwnd),
winxpgui.GetWindowText(hwnd))

winxpgui.EnumChildWindows(app_handle, enum_child, '')
When you run the whole script (source), you get:
Test application handle: 2949744
2621886 WindowsForms10.BUTTON.app.0.378734a: Add Item
7668314 WindowsForms10.STATIC.app.0.378734a: New listbox item
4194952 WindowsForms10.EDIT.app.0.378734a:
2753142 WindowsForms10.LISTBOX.app.0.378734a:
3342924 WindowsForms10.BUTTON.app.0.378734a: Quit
Here is the catch. How shall we identify Add Item button from Quit button? Both have the same class name WindowsForms10.BUTTON.app.0.378734a. Sure, the text differs, but this isn't always true. Imagine two text boxes - both having the same class name WindowsForms10.EDIT.app.0.378734a. What then? Of course, you can check positions. But this is leads you back to record/play tools and that is exactly what I want to avoid.

So this is the terminus for Win32 API. Next time - Accessibility.

3 comments:

Anonymous said...

What version of Python was this done with? I noticed the 'prints' don't play nice with 3.2.2 and I'm worried other parts (that are less easy to fix) won't work...

Any help appreciated!

Lukáš Čenovský said...

This was Python 2.x but it should not be a big problem to port it to Python 3.x - just try it and you will see.

I Hay said...

This is a great post thanks for writing it