UI automation is a programmatic interface to external applications that needs programmatic access to the application. TX Text Control provides a very powerful API to developers who integrate the libraries into their applications. But this interface is not open to other applications from outside the application. Primarily, UI automation is used for the Windows accessibility features such as screen readers and third-party applications like speech recognition software packages.

The UI automation framework provides a streamlined way to discover UI components in an application. With version X15 of TX Text Control .NET for WPF, the UI automation framework interface is implemented and provides the following advantages:

  • Enabled accessibility features
    Accessibility is getting more important and applications must be compliant with government regulations.
  • Automated UI testing
    Applications that support the UI automation interface can be tested automatically. This can be very helpful for costly and time consuming regression testing.

UI automation provides a common interface to communicate with applications that implement this interface. The automation object is provided using the System.Windows.Automation.AutomationElement class connected with an UIElement. There are several control patterns that expose functionality which can be used from outside.

TX Text Control supports 3 patterns:

  • ValuePattern
    Represents a control that has an intrinsic value that does not span a range and can be represented as a string. This string may or may not be editable depending on the control and its settings.
  • TextPattern
    Represents controls that contain text.
  • SynchronizedInputPattern
    Represents objects that support synchronized input events.

How to Use the UI Automation Interface?

The following method starts an external application and returns the parent AutomationElement:

private AutomationElement StartApp(string app)
{
p = Process.Start(app);
Thread.Sleep(2000);
return (AutomationElement.FromHandle(p.MainWindowHandle));
}
view raw automate.cs hosted with ❤ by GitHub

This method returns the first AutomationElement in the UI tree that supports the TextPattern (TX Text Control in this case):

private AutomationElement GetTextElement(AutomationElement targetApp)
{
PropertyCondition cond =
new PropertyCondition(
AutomationElement.IsTextPatternAvailableProperty,
true);
AutomationElement targetTextElement =
targetApp.FindFirst(TreeScope.Descendants, cond);
return targetTextElement;
}
view raw cond.cs hosted with ❤ by GitHub

The following code snippet uses the above methods to start a test application and to retrieve the TX Text Control AutomationElement. If the element supports the ValuePattern, a text is set.

// start the application
AutomationElement element = StartApp("tx_wpf_client.exe");
// get TX Text Control
AutomationElement textElement = GetTextElement(element);
object valuePattern = null;
if (textElement.TryGetCurrentPattern(
ValuePattern.Pattern, out valuePattern))
{
((ValuePattern)valuePattern).SetValue("This is text");
}
view raw test.cs hosted with ❤ by GitHub

The next code snippet selects the complete text in a DocumentRange:

if (textElement.TryGetCurrentPattern(
TextPattern.Pattern, out textPattern))
{
TextPatternRange currentSelection = ((TextPattern)textPattern).DocumentRange;
currentSelection.Select();
}
view raw test.cs hosted with ❤ by GitHub

Stay tuned for more features of TX Text Control X15.