Avoiding the mouse

Mar 11, 2009 at 5:41 PM
In some cases, you can avoid using the mouse to invoke a control:
CheckBox cbx = ...;
cbx.Click();

becomes:
CheckBox cbx = ...;
AutomationElement element = cbx.AutomationElement;
element.SetFocus();
object obj;
if (element.TryGetCurrentPattern(TogglePatternIdentifiers.Pattern, out obj))
{
    TogglePattern pattern = (TogglePattern) obj;
    pattern.Toggle();
}
else
{
    throw new Exception("Checkbox is not toggleable");
}


If you have an event handler on the checkbox's Clicked event, it won't get raised. However, most of the time, you'll put the event on CheckedChanged, which should cover this.

Similarly, with menu items, you use InvokePattern and InvokePatternIdentifiers.Pattern in place of TogglePattern and TogglePatternIdentifiers.Pattern.

Using these should allow you to run acceptance tests with White on a headless server, such as with CruiseControl.NET.

This isn't generally applicable. However, it covers a lot of common cases without requiring that the window being tested always have focus, and without requiring VNC or a virtual machine.
Coordinator
Mar 25, 2009 at 10:55 AM
true.
This is something definitely worth putting out there. I have an issue regarding this in issue tracker.
The only issue is that the patterns are less reliable than mouse for all frameworks except WPF.