We have learned in the previous part how to explore the tested application and read its values. To test it, we also need set values to its fields.
The first idea may be to utilize the .NET objects. When we can read values from the tested application fields, we can also set them. Use the code snippet from the previous part to run the tested application and follow it by command
App.Controls.Text = 'GUIATtext'
The tested application is started and GUIATtext appears in the New listbox item text box.
Well, it does not appear as a proper testing to me. I'd rather simulate user's interaction with mouse and keyboard. Unfortunately, sending key strokes and mouse events is not job for .NET framework. We have to dive deep into Windows and use Win32 API to perform these tasks.
I have created a simple DLL to make Win32 API functions available for IronPython . Moreover, I have added functions to simulate user's input. Here is the list of functions in the Win32API namespace with a short description:
GetForegroundWindow()return handle of the active window
SetForegroundWindow(handle)set the active window to the window with handle return boolean
ShowWindow(handle, state)set window with handle to state (minimalized, maximalized, ...) return boolean
GetWindowText(handle, title, capacity)return title of window with handle, title variable type is String of capacity size
MouseClick(X, Y)simulate click with left mouse button on position X, Y
SendKey(virtual_key)simulate pressing virtual_key
SendString(string)simulate typing a string
Using these functions is straightforward in IronPython. Just download the Win32API.dll and look at the example below how to perform a mouse click on the specific position:
import clr clr.AddReference('Win32API') from Win32API import Win32API Win32API.MouseClick(10, 10)
The code snippet moves mouse to the coordinates 10, 10 (that is to the top-left corner of the screen) and perform a click with the left mouse button. More useful examples will follow in next parts.
Finally, couple of warnings. The simulated mouse click is caught by the top most window on the position. And the simulated key strokes are sent to the active window. That means we have to pay attention to position and active state of the tested application.
In the next part, we start building the GUIAT testing framework.
 I do not know how to access Win32 API directly from IronPython.