The first version implemented the BaseComponent class to represent windows components. We could not do anything but find out location of the component. It would be nice to be able write text into a text box or click a button by simple method, wouldn't it?
To do that, we need to know the type of each component. The type name of .NET components is stored in component.GetType().Name. Knowing the type, we create subclass of BaseComponent for each type. In the subclass, we provide nice methods and properties.
First, we enhance BaseComponent class. We add _CheckVisibility(self) method that checks whether the component is visible. If not, it raises an exception. We need it for checking if button is visible so we can click it or text box is visible to write it. And we add guiat property to be able to activate the tested application from a component by calling self.guiat.Activate().
Let's look on a button. The Button class is very simple:
from time import sleepThe Click(self) method activates the tested application, checks whether the component is visible, finds out the position of the button, clicks into the middle of the button position, and finally waits a little bit. I do not like the waiting but it is here for safety. Windows may repainting some areas or do some other cool thinks, so it is better to give them some time for it.
from Win32API import Win32API
from BaseComponent import BaseComponent
"""interface to the Button component"""
""" perform left mouse click on the center of the button, no parameters"""
pos = self.location
Win32API.MouseClick(pos + (pos/2), pos + (pos/2))
The TextBox class provides three methods and one property:
The method essentially does the same as Button's Click method. It activates the tested application, checks whether the component is visible, and clicks into the middle of the TextBox position to focus the editor.
The method focuses the text box, moves cursor to the start position simulating pressing Home key, and simulates pressing Delete key until the text in text box is empty. If the text is not empty then, it raises exception.
- Write(self, aString)
The method clears the text box and uses Win32API method SendString to simulate typing aString. Then checks whether the text box contains the aString value and raises exception if not.
The property contains the actual value of the text box. When user assigns a string to it, the string is written to the text box.
Let's try a small example:
>>> import GUIATThe result of the above small script is a new line in the list box. You see that the button, label, and text box are know component types. The label is treated as BaseComponent (see RecognizableComponents in Form.py). The list box is unknown component.
>>> g = GUIAT.GUIAT()
"lbxItems" (ListBox) - UNKNOWN
Starting GUIAT done.
>>> txt = g.mainForm._GetComponentByName('txtNewItem')
>>> btn = g.mainForm._GetComponentByName('btnAddItem')
>>> txt.value = 'Hello world!'
Today, we have shown how to create button and text box GUIAT classes that control respective .NET components. We can already script our small tested application!
Next time, we add list box class and logging.