0

In my test application, I am constantly opening and re-opening a form. Everytime the form is opened, I must get all the elements on the form into an AutomationElementCollection so that I can operate on the elements. However, it seems expensive to repeatedly get these elements (due to tree navigation/context-switches etc.).

I attempted to set a boolean around the method that gets the elements. If the method was called for the first time, it would run normally, and set the boolean to true. If the method gets called a second time it will do nothing, as the array has already been populated.

However when I try to perform operations on any AutomationElement in the array (for a second time), the elements do not seem to be available. Does closing the form somehow "disable" these elements? Do I HAVE to find these elements each time I open the form, so that they are "fresh"?

I looked at the CacheRequest way, but that seems to only pertain to accessing properties/patterns, not elements.

Here is code/error message:

AutomationElement GAP;
AutomationElementcollection GAP1;
private bool initGAP1 = false;
    public void initGAP()
    {
        if (!initGAP1)
        {
            int refnum = ...;
            int refnum2 = ...;
            AutomationElementCollection temp = MMChildren[refnum].FindAll(TreeScope.Children, findCondition);
            GAP = temp.FindAll(TreeScope.Children, findCondition)[refnum2];
            GAP1 = GAP.FindAll(TreeScope.Children, findCondition); //this contains the elements I want to operate on
            initGAP1 = true;
        }
    }

System.Windows.Automation.ElementNotEnabledException: Exception of type 'System.Windows.Automation.ElementNotEnabledException' was thrown.

jpints14
  • 1,361
  • 6
  • 15
  • 24

1 Answers1

2

You would need to re-get the Automation Elements for each new window. As I understand the UI Automation framework it gives you the means to investigate running windows. It will collect information with different techniques, depending on what kind of framework the target application uses. In your case, if you create and destroy instances of windows, they are treated as different AutomationElements since they are different windows (basically they have different window handles in the OS). Even if the underlying controlling code is the same they are different instances towards the OS, and therefore UI automation.

If you experience that you are suffering from the performance in the traversion, it might be worth considering to use the UI Automation COM API instead, that is vastly faster on some operations.

  • Yeah, this is accurate. Each time you reopen that window, that's a brand new set of AutomationElements. I second the recommendation of trying the native COM API, it generally tends to perform better than the managed API. Also, make sure your search algorithms are as efficient as you can get them (i.e. limit scope, use FindFirst instead of FindAll where appropriate, use caching where possible, etc.). – Chaser324 Jun 01 '12 at 23:54