Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify Contains in UI Automation PropertyCondition

I am using UI Automation for GUI testing.

My window title contains the application name appended by a filename.

So, I want to specify Contains in my Name PropertyCondition.

I checked the overload but it is related to Ignoring the Case of the Name value.

Can anyone let me know how to specify Contains in my Name PropertyCondition?

Regards,

kvk938

like image 875
kvk938 Avatar asked Oct 10 '14 05:10

kvk938


People also ask

What is the use of Ui properties in UI Automation?

Properties on IUIAutomationElement objects contain information about UI elements, usually controls. The properties of an element are generic; that is, not specific to a control type. Control-specific properties of an element are exposed by its control pattern interfaces. Microsoft UI Automation properties are read-only.

Where can I find the latest information about UI Automation?

For the latest information about UI Automation, see Windows Automation API: UI Automation. This article contains example code that shows how to locate an element within the UI Automation tree based on a specific property or properties.

What happens if UI automation provider does not implement a property?

If a UI Automation provider does not implement a property, UI Automation can supply a default value. For example, if the provider for a control does not support the property identified by UIA_HelpTextPropertyId, UI Automation returns an empty string.

How do I set control-specific properties of an element?

Control-specific properties of an element are exposed by its control pattern interfaces. Microsoft UI Automation properties are read-only. To set properties of a control, you must use the methods of the appropriate control pattern. For example, use IUIAutomationScrollPattern::Scroll to change the position values of a scrolling window.


1 Answers

I've tried solution of Max Young but could not wait for its completion. Probably my visual tree was too large, not sure. I've decided that it's my app and I should use knowledge of concrete element type I am searching for, in my case it was WPF TextBlock so I've made this:

public AutomationElement FindElementBySubstring(AutomationElement element, ControlType controlType, string searchTerm)
{
    AutomationElementCollection textDescendants = element.FindAll(
        TreeScope.Descendants,
        new PropertyCondition(AutomationElement.ControlTypeProperty, controlType));

    foreach (AutomationElement el in textDescendants)
    {
        if (el.Current.Name.Contains(searchTerm))
            return el;
    }

    return null;
}

sample usage:

AutomationElement textElement = FindElementBySubstring(parentElement, ControlType.Text, "whatever");

and it worked fast.

like image 156
sarh Avatar answered Oct 15 '22 18:10

sarh