Virus
Virus

Reputation: 3415

MS UI Automation - How to get text from ControlType.text

I have a small windows application that has a series of labels on it. This application will be globalized and there is a possibility that the text on these labels might get truncated. I am trying to automate to identify truncated text on these labels.

For other controls, I can use TextPattern.Pattern through which I can find the visible text and the actual text inside the control. But for the labels (ControlType.text) the TextPattern is not supported. How do I find the visible text for these lables using UI automation.

Here is the code I tried. If I pass control type as Document it works. But with Text control type it gives a unsupported pattern exception.

private String TextFromSelection(AutomationElement target, Int32 length)
        {
            // Specify the control type we're looking for, in this case 'Document'
            PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text);

            // target --> The root AutomationElement.
            AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

            TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

            if (textpatternPattern == null)
            {
                Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
                return null;
            }


            var test = textpatternPattern.DocumentRange.GetText(-1).TrimEnd('\r');

            var tpr = textpatternPattern.GetVisibleRanges();
            var txt = tpr[0].GetText(-1);

            return txt;
        }

Upvotes: 2

Views: 5168

Answers (2)

Guy Barker - Microsoft
Guy Barker - Microsoft

Reputation: 574

Whether the text pattern is supported by the label element will be affected by which UI framework is being used. For example, the labels in the Win32 Run dlg don't support the Text pattern, but labels in the Windows 10 XAML Calculator do. The image below shows the Inspect SDK tool reporting that the Text pattern is supported by the "There's no history yet" label.

It's important to note that whether the UI framework (or the app if the app implements the UIA Text pattern directly) includes the truncated text in the data it returns to you when you call IUIAutomationTextPattern::GetVisibleRanges(), is up to the framework (or app) itself. For example, WordPad running on Windows 10 doesn't include the text that's clipped out of view, but Word 2013 does return the clipped text.

Thanks,

Guy

enter image description here

Upvotes: 3

Simon Mourier
Simon Mourier

Reputation: 138841

You should be able to simply use element.Current.Name (element being the instance of AutomationElement for the label).

Here is an example of UISpy retrieving the text for a label:

enter image description here

Upvotes: 2

Related Questions