简体   繁体   中英

UIAutomation — Strange ExpandCollapse Behavior

I'm trying to automate menu picks using Microsoft UI Automation.

The application that I'm targeting is a Windows application that includes an embedded browser. The application has a menu bar with File, Edit, etc. menu items.

The machine on which I'm running the automation is a Windows XP machine.

Here's the code I'm using to expand the File menu item:

AutomationElement aeMenuBar = 
            aeFormElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuBar));


        AutomationElement aeFileItem = 
            aeMenuBar.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "File"));


        ExpandCollapsePattern fileECPattern = aeFileItem.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
        fileECPattern.Expand();

I get an InvalidOperationException when I hit the Expand() method.

I can't figure out what's causing the Exception. The ExpandCollapseState of fileECPattern is Collapsed, which I believe is correct (in other words, it's not a LeafNode).

One thing I do notice is some anomalous behavior in the target application when I view it using UI Spy. In particular, if in UI Spy I select the File menu item, "Scope to Element", then run the Expand method, the File menu doesn't expand. Instead, the menu item next to File (ie, Edit) expands. This same behavior happens with other menu items as well. It doesn't happen when I change applications (eg, Microsoft Paint) -- everything seems to work fine there.

On some basic level, the menu items in my target application just don't seem to be playing well with UI Automation and UI Spy.

Any suggestions as to why not would be greatly appreciated.

Menu bars in some apps are tricky; they're often really just "toolbarwindow32" toolbar common controls where the app will call TrackPopupMenu to display a submenu when the menu item is pressed - and there's really no actual link between the drop-down and the menu bar except for a line or two of code within the app. UIAutomation does a 'best attempt' at making Expand/Collapse work for the menus it knows about (eg the User32 type you find in pre-Win7 Notepad), but doesn't always work elsewhere.

Fallbacks you can try: see if Invoke works as a way of making the submenu appear; failing that, try getting a clickable point and click the mouse at that position (using SendInput).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM