我需要一种方法来检查代码库中的每个 WPF 控件是否都有 AutomationProperties.AutomationId 属性。 (这是自动化 UI 测试所必需的。)我还需要确保将来在可能添加到代码库的所有未来表单上强制执行此要求。我考虑了几种不同的方法,但到目前为止,没有一种方法是正确的。
选项 1:为应用程序中的每个 WPF 表单编写单元测试。
这是一个相当简单的方法。我已经编写了一个方法,该方法检查 DependencyControl 的所有逻辑后代并确保每个后代都有一个 AutomationId。这是一些 C#:
private bool AllControlsHaveAutomationId(DependencyObject control)
{
bool result = true;
if (this.ControlHasAutomationId(control))
{
foreach (object o in LogicalTreeHelper.GetChildren(control))
{
FrameworkElement frameworkElement = o as FrameworkElement;
if (frameworkElement != null)
{
result = this.AllControlsHaveAutomationId(frameworkElement);
}
}
}
else
{
result = false;
}
return result;
}
private bool ControlHasAutomationId(DependencyObject control)
{
return !string.IsNullOrEmpty(AutomationProperties.GetAutomationId(control));
}
这种方法的问题在于,我假设其他开发人员会为其表单编写单元测试,这不是一个好的假设。如果我不能假设 AutomationIds 将添加到所有 WPF 控件中,那么我也不能假设将编写单元测试。
选项 2:使用 System.Xml.XmlDocument 读取每个 XAML 文件。检查文档中 AutomationProperties.AutomationId 的元素。
这种方法似乎很有前途。我最初的计划是将每个 XAML 文件加载到 XmlDocument 中,并检查文档中每个元素的 AutomationProperties.AutomationId 属性。但是,我意识到并非 XAML 中的每个元素类型都支持 AutomationProperties.AutomationId 属性。我需要一种读取 XAML 文件中的元素并确定该元素是否支持 AutomationProperties.AutomationId 属性的方法。有没有办法做到这一点?
您可以查看代码库所有程序集中的 BAML(编译的 XAML)......有多种方法可以做到这一点。
通过 .NET Reflector 和 AddIn 查看 BAML
一种方法是编写一个 Reflector 插件。
您也许可以将其基于已经可用的 BAML 查看器。
然后希望这只是给 Reflector 你的组件并说在没有自动化 ID 的情况下给我找东西的情况。
通过 StylesExplorer 查看 BAML
- http://stylesexplorer.codeplex.com/ http://stylesexplorer.codeplex.com/
允许您使用程序集访问 BAMLBamlAssembly
class.
通过 LocBAML 查看 BAML
http://msdn.microsoft.com/en-us/library/ms746621.aspx http://msdn.microsoft.com/en-us/library/ms746621.aspx
http://bloc.codeplex.com/releases/view/44124 http://bloc.codeplex.com/releases/view/44124(相关链接)
Windows SDK 中提供了该工具的源代码。您可以进行修改以查找自动化属性而不是可本地化的属性。
使用 XamlReader.Load()
你甚至可能只需要到达某个地方XamlReader.Load()
与未编译的 .xaml 文件一起使用。
- http://go4answers.webhost4life.com/Example/xamlreaderloadbaml-removed-125290.aspx http://go4answers.webhost4life.com/Example/xamlreaderloadbaml-removed-125290.aspx
使用 XamlReader.LoadBaml()
- http://wpftutorial.net/baml.html http://wpftutorial.net/baml.html
其他选项
各种自动化的运行时验证器
- http://acccheck.codeplex.com/releases/view/46527 http://acccheck.codeplex.com/releases/view/46527
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)