如何在不破坏自动化测试的情况下为 TabControl 定义 ControlTemplate?
总结
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不同但相似的 msdn 论坛问题的不同 MSFT 响应中找到答案,UI 自动化缺少 TabControl 控件。
要使 UI 自动化适用于 ContentTemplated TabControl,请将 Name="PART_SelectedContentHost" 属性添加到 ContentPresenter,如下
所示 就这样了。 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
That's all it takes. UISpy can now see the content of the currently selected tab.