WPF Application UI Test Automation – Working with Menu Controls

I’ve been looking at testing WPF (Windows Presentation Foundation) applications lately. It took me a while to warm up to WPF apps but now I’m a big fan. UI automation for WPF apps is a new ball game. By far the best approach is to use the Microsoft UI Automation (MUIA) library which, like WPF, is part of the .NET 3.0 Framework. One thing I especially about WPF applications is the new Menu control model. Classic WinForms use a completely separate model for the main application menu and non-menu user controls. The idea was back then that menu-ing was really hooking directly into low-level Windows API functions so the menu control model should be different from the model forother user controls. See the screenshot below for an example of automating WPF Menu controls. Then at some point .NET revised the menu model (I think in .NET Framework 2.0 as I recall) to use a menuStrip control but this just added mild confusion (to me anyway). But WPF applications treat Menu controls just like any other user controls. This makes them much easier to automate than classic WinForm, non-WPF, menu controls. Unfortunately, in Visual Studio 2008 there is no built in support for drag-and-drop design for menu controls so you have to code the XAML by hand. But it’s not hard. For example, the automation in the screenshot below is exercising a WPF app with this menu XAML I added by hand:
<Menu Height="22" Name="menu1"
  VerticalAlignment="Top" IsMainMenu="True" >
  <MenuItem Header="_File">
    <MenuItem Header="_New" Name="fileNew" />
    <MenuItem Header="_Open" Name="fileOpen" />
    <Separator />
    <MenuItem Header="E_xit" Name="fileExit"
     InputGestureText="Alt-F4" ToolTip="Exit StatCalc"
     Click="OnFileExit" />
  <MenuItem Header="_Help">
    <MenuItem Header="Help Topics" />
    <Separator />
    <MenuItem Header="About StatCalc" />
Notice the Click="OnFileExit" in the UI design. At compile time this will generate a C# method:
public void OnFileExit(object sender, RoutedEventArgs e)
  // TODO
and you can simply add: this.Close(); to the method body. Neat. Anyway, test automation for WPF apps is very interesting. I am writing up an article for MSDN Magazine that addresses all the key issues.


This entry was posted in Software Test Automation. Bookmark the permalink.