简体   繁体   English

UIAutomation —奇怪的ExpandCollapse行为

[英]UIAutomation — Strange ExpandCollapse Behavior

I'm trying to automate menu picks using Microsoft UI Automation. 我正在尝试使用Microsoft UI自动化来自动执行菜单选择。

The application that I'm targeting is a Windows application that includes an embedded browser. 我所针对的应用程序是一个包含嵌入式浏览器的Windows应用程序。 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. 我在其中运行自动化程序的计算机是Windows XP计算机。

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. 当我点击Expand()方法时,我得到一个InvalidOperationException。

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). fileECPattern的ExpandCollapseState为Collapsed,我认为这是正确的(换句话说,它不是LeafNode)。

One thing I do notice is some anomalous behavior in the target application when I view it using UI Spy. 我确实注意到的一件事是,当我使用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. 特别是,如果在UI Spy中选择“文件”菜单项“范围到元素”,然后运行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. 当我更改应用程序(例如Microsoft Paint)时,它不会发生-一切似乎都可以正常工作。

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. 在某些基本级别上,我的目标应用程序中的菜单项似乎与UI Automation和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. 它们通常实际上只是“ toolbarwindow32”工具栏常用控件,在该控件中,应用程序将在按下菜单项时调用TrackPopupMenu来显示子菜单-下拉菜单和菜单栏之间实际上没有实际链接,只有一两行应用内的代码。 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. UIAutomation尽最大努力使它知道的菜单(例如,您在Win7记事本中找到的User32类型)展开/折叠工作,但并不总是在其他地方工作。

Fallbacks you can try: see if Invoke works as a way of making the submenu appear; 您可以尝试使用后备功能:查看Invoke是否可以使子菜单出现; failing that, try getting a clickable point and click the mouse at that position (using SendInput). 否则,请尝试获得可点击的点,然后在该位置单击鼠标(使用SendInput)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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