UI Automation Performance

329 Views Asked by At

I see that traversing UI Tree of of any Application, that supports UI Automation and has about 1k elements, takes time in excess of 8 seconds at least, using UI Automation COM library. Tree Walk is done using Control Walker. I understand it involves inter-process communication to make a call like FindAll and then retrieve further properties.

The response time differs from Application to Application, however, the best I see is still more than aforementioned time.

I tried UI Automation Caching (below sample), however, the initial Tree walk still incurs cost of over 7-8 seconds as well Caching the Parent's properties is not a possibility (or at least I could not get it from Cache even using different types of traversals like TreeScope Descendants and Element or a combination). Just a sample code where I try to Create Condition array on all the available Controls and expecting some Properties to be Cached. The Application has about 1500 controls of which about 800 are visible. It takes roughly 9 seconds for Cache to build up.

    private static IUIAutomationCacheRequest BuildCacheRequest()
    {
        IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest();
        cacheRequest.AddProperty(UIA_PropertyIds.UIA_ControlTypePropertyId);
        cacheRequest.AddProperty(UIA_PropertyIds.UIA_AutomationIdPropertyId);
        ....
        return cacheRequest;
    }       
    private static IUIAutomationCondition BuildConditionArray()
    {
        IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[40];
        int controlType = 50000;

        for (int i = 0; i < 40; i++)
        {
            conditionArray[i] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, controlType);
            controlType += 1;
        }

        return _automation.CreateOrConditionFromArray(conditionArray);
    }
    
    private static IUIAutomationElementArray FindAllDescendantsBuildCache(IUIAutomationElement element, IUIAutomationCacheRequest cacheRequest, IUIAutomationCondition conditionArray)
    {
        IUIAutomationCondition[] condArr = new IUIAutomationCondition[2];
        condArr[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsOffscreenPropertyId, false);
        condArr[0] = conditionArray;
        cacheRequest.AutomationElementMode = AutomationElementMode.AutomationElementMode_None;
        cacheRequest.TreeFilter = _automation.ControlViewCondition;

        var cachedElements = element.FindAllBuildCache(TreeScope.TreeScope_Descendants,
         _automation.CreateAndConditionFromArray(condArr), cacheRequest);
        return cachedElements;
    }

Once Cached, response time is much faster, understandably.

Question I have -
Is there any possibility to Traverse UI Tree carrying about 2k elements within a second or two using UI Automation (or even otherwise)? I also look to identify the traversed Elements through their properties, just like Properties' support from UI Automation

Just as a side note, I have tried to use Win32 API at basic level like using EnumChildWindows, however, even that doesn't yield Control handles on different Types of Desktop Frameworks like Electron or WPF.

0

There are 0 best solutions below