白名单/ UI自动化无法识别选项卡中的容器(窗格控件)

时间:2014-09-02 07:10:01

标签: ui-automation white

我自动化Windows应用程序。我试图在选项卡控件下访问窗格元素(具有文本框,组合框控件),但它无法访问。白色返回null。

我尝试了其他技术,如UI自动化TreeWalker(Rawview,控制视图,内容视图),但没有任何帮助。

参考以下链接中的图片: https://dl.dropboxusercontent.com/u/68446125/Tab.png https://dl.dropboxusercontent.com/u/68446125/General%20Pane.png

如图1所示,White / UI Automation正确检索了选项卡控件,但未返回子元素General * Pane,并且无法访问它的控件(参见图2突出显示),第一个可访问的子项element是" General * tab Item"。

奇怪的是,这些控件可以在Inspect.exe中访问(在Windows SDK中)。我尝试了以下方法来检索控件,但是永远不能通过White / UI Automation访问General * Pane。

var tab = Window.Get<Tab>(SearchCriteria.ByControlType(ControlType.Tab).AndByClassName("TwoPageControl")); // Tab control is retrieved properly
var pane = tab.GetElement(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*")); // this line returns NULL

var pane1 = revWindow.GetElement(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*")); // this line returns NULL
var pane2 = revWindow.Get<Panel>(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*"));// throws exception "Failed to get ControlType=pane,Name=General*,ControlType=pane"

尝试了Windows UI自动化代码,但没有运气。

 System.Windows.Automation.Condition cd1 = new AndCondition(
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab),
                new PropertyCondition(AutomationElement.ClassNameProperty, "TwoPageControl"));     

            AutomationElement a = window.FindFirst(TreeScope.Descendants, cd1); // Tab control is returned properly here

          TreeWalker rawViewWalker = TreeWalker.RawViewWalker;
            AutomationElement cc = rawViewWalker.GetFirstChild(a); // General * Pane is not returned, instead General* Tab item is returned, though it's not the first child.
            var cd = rawViewWalker.GetNextSibling(cc);  // this returns next pane element available, not General * Pane.               

请帮助我如何在标签控制下访问General * Pane及其子女。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我的应用程序遇到了完全相同的问题。我使用Inspect和开源UIAVerify,其中Pane元素作为tab元素的子元素可见。但是当我将Verify编译为.Net 4.5项目时,Pane元素并未被视为标签的一部分。只有当我直接指出它时才会出现。我也在主窗口的后代中搜索我的Pane元素,但什么也没有。我认为这与动态创建该窗格内容有关(我的意思是当您选择不同的tabItem时会有不同的内容)。

我认为从树的角度来看,你无法访问该元素。

我的解决方案是使用AutomationElement.FromPoint方法。 http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.frompoint(v=vs.110).aspx

如果你和开发程序的人发生争执,我也认为这会有所帮助。 Some controls on a page are not visible for MS UI Automation

答案 1 :(得分:0)

您可以将TabControl分解为TabPanel,但不包含其ContentPresenter。您可以创建自己的ContentPresenter,它使用所选TabItem的内容。

这样,White就能够发现ContentPresenter中的控件。

这是一种解决方法,很遗憾你必须更改你的WPF代码只是因为“UIA出了问题”。但这是我能做的最好的事情。

<Grid>

    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TabControl x:Name="uiTab" Grid.Row="1">

      <TabControl.Template>
        <ControlTemplate TargetType="TabControl">
          <TabPanel IsItemsHost="True" VerticalAlignment="Stretch"/>
        </ControlTemplate>
      </TabControl.Template>

      <TabItem Header="first">
        <Button>FIRST</Button>
      </TabItem>
      <TabItem Header="second">
        <Button>SECOND</Button>
      </TabItem>

    </TabControl>

    <ContentPresenter Content="{Binding SelectedItem.Content, ElementName=uiTab}"/>

  </Grid>
相关问题