After a while, it's time to continue building the GUI Automated Testing framework. Today, I focus on a list box. You can download the source as usual.
I assume standard windows list box component where only unique items are stored and only one item can be selected. These two conditions are set only because of simplicity of the example. If you don't like them, enhance the example by yourself as your homework :-)
Select(self, aItem)The method selects aItem in the list box.
def Select(self, aItem): """ select an item from list box @aItem - string with item to select """ if aItem not in self.items: raise Exception("Item '%s' not in list box" % aItem) self.guiat.Activate() self._CheckVisibility() pos = self.location # click on the first item to focus the list box Win32API.MouseClick(pos + (pos/2), pos + 3) # send Home to be sure we are on the first item # (we could be scrolled down a little) Win32API.SendKey(Win32API.VK_HOME) # simulate pressing down arrow until we find the item # we should find it because it is among self.items while self.value != aItem: Win32API.SendKey(Win32API.VK_DOWN)
First, it checks whether aItem is in the list box and raises an exception if not. Then it clicks onto the first visible list box item, simulates pressing Home key to focus the first item and repeatedly press Down key until the value equals aItem.
This approach has the advantage we don't need to care about scroll box. The basic windows list box does not provide nice properties or methods to return its state. We would have to dive into Win32 API to find it. For example, the DevExpress ListBoxControl has method GetViewInfo that returns information about internal list box state.
This brings us to the important note:
We do not test the list box component. We test the application.
One way of selecting item in a list box is enough. Of course, we must be aware of its limitations and side effects. Selecting an item with our GUIAT component fires one OnClick and several OnChange events.
itemsThe property contains read only list of all items in the list box.
valueThe property contains the selected item in the list box. User can assign a string to it to select the string in the list box.
This is the last post about controlling components. You know the idea so developing a new GUIAT class controlling your component should be easy.
Next time, I show how to control application that is already started.