Thursday, October 30, 2008

Building the framework (3)

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 :-)

The list box component is again a subclass of BaseComponent. It is quite simple - it has one method and two properties:

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[0] + (pos[2]/2), pos[1] + 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.

items

The property contains read only list of all items in the list box.

value

The property contains the selected item in the list box. User can assign a string to it to select the string in the list box.

Incorporating the new GUIAT ListBox class into the framework is easy - just add it into the RecognizableComponents dictionary of the Form class.

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.