In a previous post I had implemented clicking through menus with White. Someone pointed out that this was a solved problem and I didn’t need to write any code.
What I noticed next is that it would take anywhere from 3 to 10 seconds to resolve mainWindow.MenuBar. I looked at the implementation and it does a descendents (DFS?) search. The menu bar is a child of the window, so we can avoid the recursion altogether.
Generalizing this further, a simple tree walker with a depth limit seems to be much more efficient for both scenarios of deep and wide trees. The following code comes from this thread, I didn’t write it.
The new test code executes magnitudes faster to click File –> Save.
Another interesting aspect of this menu bar is that it is virtualized. This means that while you may be holding an instance of a MenuBar, it may have been hidden by another UI element at some point and will now throw an exception with the UIA_E_ELEMENTNOTAVAILABLE error code if you try to use it. This is annoying: if I need to click on 10 menu items, I have to do 10 searches, starting from the top every time! To solve this, .NET 4.0 has introduced a new IUIAutomationVirtualizedItemPattern that has a Realize method to materialize the object again.