Using FitNesse for integration test can be great, but sometimes you want to test the front end WPF components and not just the presenter.
Referencing the .NET 3.0 UIAutomation assemblies allows you to programatically control you WPF application. The only trouble I had was that the builtin FindFirst() method was really slow and unpredictable so after switching to the TreeWalker everything went fast and smooth.
private static AutomationElement Find(AutomationElement element, string name, int maxDepth)
{
if (maxDepth == 0)
{
return null;
}
TreeWalker walker = TreeWalker.RawViewWalker;
AutomationElement current = walker.GetFirstChild(element);
while (current != null)
{
if ((string)current.GetCurrentPropertyValue(AutomationElement.NameProperty) == name || (string)current.GetCurrentPropertyValue(AutomationElement.AutomationIdProperty) == name)
{
return current;
}
current = walker.GetNextSibling(current);
}
current = walker.GetFirstChild(element);
while (current != null)
{
AutomationElement found = Find(current, name, maxDepth - 1);
if (found != null)
{
return found;
}
current = walker.GetNextSibling(current);
}
return null;
}
Now you can for instance set the value of a textbox by using:
public static void SetTextBoxValue(AutomationElement mainWindow, string id, string value)
{
AutomationElement textBox = FindElement(mainWindow, id);
ValuePattern valuePattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);
valuePattern.SetValue(value);
}