如何在不破坏自动化测试的情况下为 TabControl 定义 ControlTemplate?

发布于 2024-09-18 08:21:09 字数 2656 浏览 3 评论 0原文

总结

在我的 WPF 应用程序中,我需要一个左侧有按钮的 TabControl,因此我定义了一个具有我想要的布局的 ControlTemplate,并且它工作得很好。

但是,我的测试人员的自动化测试工具看不到选项卡的任何内容,包括当前选定的选项卡。

问题:如何保持 TabControl 可通过自动化测试工具进行测试,同时仍定义 ControlTemplate?


详细信息

我正在使用 WPF 3.5 开发 WPF 应用程序
我的测试人员正在使用名为 QTP
的自动化测试工具 他说他可以用 UISpy.exe 测试你能看到的任何内容

  • 。我使用没有应用模板的直接 TabControl,UISpy 可以看到当前所选选项卡的内容。
  • 但是,当我使用 ContentTemplate 更改布局(代码如下所示)时,UISpy 仍然可以看到选项卡标题...但它看不到内容。

示例 WPF 应用程序(Xaml):

<Window x:Class="TabControlTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Tab Control Test"
        Height="300"
        Width="300">
    <Window.Resources>
        <ControlTemplate x:Key="ButtonsOnLeftTabLayout"
                         TargetType="{x:Type TabControl}">
            <DockPanel>
                <StackPanel DockPanel.Dock="Left"
                            IsItemsHost="True" />
                <ContentPresenter Content="{TemplateBinding SelectedContent}" />
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>
    <TabControl Template="{StaticResource ButtonsOnLeftTabLayout}">
        <TabItem Header="Tab 1">
            <StackPanel>
                <Button HorizontalAlignment="Center">Button 1</Button>
            </StackPanel>
        </TabItem>
        <TabItem Header="Tab 2">
            <StackPanel>
                <Button HorizontalAlignment="Center">Button 2</Button>
            </StackPanel>
        </TabItem>
    </TabControl>
</Window>

到目前为止我的搜索发现:

  • 一堆关于必须使用自定义 AutomationPeer 编写自定义 TabControl 的内容(例如 MSFT 对论坛问题的回答 UI 自动化:访问 ControlTemplate 中的控件,博客文章 自定义控件和 UI 自动化) 。但我的直觉告诉我,这太疯狂了,“必须有一个更简单的方法!”
  • 关于为 ContentPresenter 指定名称、x:Name 或 AutomationProperties.AutomationId 的一些建议 - 这些都没有任何效果

(搜索后我终于找到了答案,但花费的时间比我想象的要长,并且 AutomationPeer早期的发现确实是不正确的,所以我将其写为一个SO问题和自我回答,以防它将来对其他人有帮助)

Summary

In my WPF application, I needed a TabControl with buttons on the left, so I defined a ControlTemplate with the layout I wanted and it worked fine.

However, my tester's automated testing tool can't see any of the content of the tabs, including the currently selected tab.

Question: How can I keep my TabControl testable by the automated testing tools, while still defining the ControlTemplate?


Details

I'm developing a WPF application using WPF 3.5
My tester is using an automated test tool called QTP
He says he can test anything you can see with UISpy.exe

  • When I use a straight TabControl with no Template applied, UISpy can see the content of the currently selected tab.
  • However, when I use a ContentTemplate to change the layout (code shown below), UISpy can still see the tab headers... but it cannot see the content.

Sample WPF application (Xaml):

<Window x:Class="TabControlTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Tab Control Test"
        Height="300"
        Width="300">
    <Window.Resources>
        <ControlTemplate x:Key="ButtonsOnLeftTabLayout"
                         TargetType="{x:Type TabControl}">
            <DockPanel>
                <StackPanel DockPanel.Dock="Left"
                            IsItemsHost="True" />
                <ContentPresenter Content="{TemplateBinding SelectedContent}" />
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>
    <TabControl Template="{StaticResource ButtonsOnLeftTabLayout}">
        <TabItem Header="Tab 1">
            <StackPanel>
                <Button HorizontalAlignment="Center">Button 1</Button>
            </StackPanel>
        </TabItem>
        <TabItem Header="Tab 2">
            <StackPanel>
                <Button HorizontalAlignment="Center">Button 2</Button>
            </StackPanel>
        </TabItem>
    </TabControl>
</Window>

What my search has found so far:

  • A bunch of stuff about having to write a custom TabControl with a custom AutomationPeer (e.g. MSFT answer to forum question UI Automation: accessing control in a ControlTemplate, blog posting Custom Controls and UI Automation). But my every instinct says that's crazy overkill, "there must be a simpler way!"
  • Some suggestions about giving the ContentPresenter a Name, or x:Name, or AutomationProperties.AutomationId -- none of which have any effect

(After search I finally found answer but it took longer than I thought it should have, and the AutomationPeer early findings were indeed incorrect, so I'm writing this as a SO question and self-answer, in case it helps anyone else in future)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2024-09-25 08:21:09

在不同但相似的 msdn 论坛问题的不同 MSFT 响应中找到答案,UI 自动化缺少 TabControl 控件

要使 UI 自动化适用于 ContentTemplated TabControl,请将 Name="PART_SelectedContentHost" 属性添加到 ContentPresenter,如下

<ContentPresenter Name="PART_SelectedContentHost"
                  Content="{TemplateBinding SelectedContent}"/>

所示 就这样了。 UISpy 现在可以查看当前所选选项卡的内容。

Found answer in a different MSFT response on a different but similar msdn forum question, TabControl controls are missing for UI Automation.

To get the UI Automation working for ContentTemplated TabControl, add Name="PART_SelectedContentHost" attribute to the ContentPresenter, like this

<ContentPresenter Name="PART_SelectedContentHost"
                  Content="{TemplateBinding SelectedContent}"/>

That's all it takes. UISpy can now see the content of the currently selected tab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文