介绍
我找到的正确阅读方式this article
这是一个很长的故事,大多数人可以跳过它:)但是那些想要了解问题和解决方案的人必须阅读这一切!
我是 QA,不久前开始负责我点击的产品的自动化。幸运的是,这个自动机不是在某些测试工具中发生,而是在 Visual Studio 中发生,因此它最大限度地接近开发。
对于我们的自动化,我们使用一个由 MbUnit(Gallio 作为运行程序)和 MINT(除 MbUnit 之外,由我们合作的客户编写)组成的框架。 MbUnit 为我们提供了测试装置和测试,而 MINT 添加了额外的更小的层——测试中的操作。例子。夹具称为“FilteringFixture”。它由大量测试组成,例如“TestingFilteringById”或“TestingFilteringWithSpecialChars”等。每个测试都由操作组成,这些操作是我们测试的原子单元。操作示例包括 -“打开应用程序(参数)”、“OpenFilterDialog”等。
我们已经有很多测试,其中包含很多动作,很混乱。他们使用我们 QA 产品的内部 API。此外,我们开始研究一种新的自动化方法 - 通过 Microsoft UI Automation 进行 UI 自动化(对不起,同义反复)。因此,对于管理者来说,某种“出口者”或“报告者”工具的必要性变得非常迫切。
前段时间,我有一个任务是开发一些应用程序,它可以解析 DLL(其中包含所有的装置、测试和操作),并以人类可读的格式(TXT、HTML、CSV、XML、任何其他格式)导出其结构。 )。但是,在那之后,我就去度假了(两周)。
碰巧的是,我的女朋友去她的家人那里直到放假(她也放假了),而我独自一人留在家里。我一直在思考我要做什么(两周),我记得“编写导出器工具任务”以及我计划开始学习 WPF 多久了。因此,我决定在假期期间完成我的任务,并编写一个 WPF 应用程序。当时我听说了一些关于MVVM的事情,我决定使用纯MVVM来实现。
可以用fixrtures等解析DLL的DLL已经写得相当快(大约1-2天)。之后我开始使用 WPF,本文将向您展示它是如何结束的。
我花了假期的大部分时间(几乎 8 天!),试图在我的头脑和代码中整理它,最后,它(几乎)完成了。我女朋友不相信我一直在做的事情,但我有证据!
用伪代码一步步分享我的解决方案,以帮助其他人避免类似的问题。这个答案更像是教程=)(真的吗?)。如果您对从头开始学习 WPF 时最复杂的事情感兴趣,我会说 - 让这一切成为真正的 MVVM 和 f*g TreeView 绑定!
如果您想要一个包含解决方案的存档文件,我可以稍后再给您,就在我做出决定时,我认为这是值得的。有一个限制,我不确定我是否可以共享 MINT.dll,它带来了 Actions,因为它是由我们公司的客户开发的。但我可以删除它,并共享该应用程序,该应用程序只能显示有关夹具和测试的信息,但不能显示有关操作的信息。
夸夸其谈的话。只需一点点 C# / WinForms / HTML 背景并且无需任何实践,我就能够在近 1 周内实现此版本的应用程序(并撰写本文)。所以,不可能也是可能的!就跟我一样放个假,花点时间去WPF学习吧!
分步教程(尚未附加文件)
任务的简短重复:
前段时间,我有一个开发应用程序的任务,它可以解析 DLL(其中包含测试装置、测试方法和操作 - 我们基于单元测试的自动化框架的单元),并以人类可读的格式(TXT 、HTML、CSV、XML、任何其他)。我决定使用 WPF 和纯 MVVM 来实现它(两者对我来说都是绝对的新事物)。对我来说最困难的两个问题是 MVVM 方法本身,然后是 MVVM 绑定到 TreeView 控件。我跳过了关于 MVVM 划分的部分,这是单独文章的主题。以下步骤是关于以 MVVM 方式绑定到 TreeView。
-
没那么重要:创建DLL,它可以通过单元测试打开DLL,并使用反射查找fixture、测试方法和动作(更小级别的单元测试,由我们公司编写)。如果您对它是如何完成的感兴趣,请看这里:使用Reflection解析函数/方法内容
-
DLL:为固定装置、测试和操作(数据模型、实体模型?)创建单独的类。我们将使用它们进行绑定。您应该自己思考,您的树的实体模型是什么。主要思想 - 树的每个级别都应该由适当的类公开,这些属性可以帮助您在树中表示模型(并且理想情况下,将作为模型或模型的一部分出现在 MVVM 中的正确位置)。就我而言,我对实体名称、子级列表和序号感兴趣。序数是一个数字,代表一个实体在DLL内部代码中的顺序。它帮助我在 TreeView 中显示序数,仍然不确定它是正确的方法,但它有效!
public class MintFixutre : IMintEntity
{
private readonly string _name;
private readonly int _ordinalNumber;
private readonly List<MintTest> _tests = new List<MintTest>();
public MintFixutre(string fixtureName, int ordinalNumber)
{
_name = fixtureName;
if (ordinalNumber <= 0)
throw new ArgumentException("Ordinal number must begin from 1");
_ordinalNumber = ordinalNumber;
}
public List<MintTest> Tests
{
get { return _tests; }
}
public string Name { get { return _name; }}
public bool IsParent { get { return true; } }
public int OrdinalNumber { get { return _ordinalNumber; } }
}
public class MintTest : IMintEntity
{
private readonly string _name;
private readonly int _ordinalNumber;
private readonly List<MintAction> _actions = new List<MintAction>();
public MintTest(string testName, int ordinalNumber)
{
if (string.IsNullOrWhiteSpace(testName))
throw new ArgumentException("Test name cannot be null or space filled");
_name = testName;
if (ordinalNumber <= 0)
throw new ArgumentException("OrdinalNumber must begin from 1");
_ordinalNumber = ordinalNumber;
}
public List<MintAction> Actions
{
get { return _actions; }
}
public string Name { get { return _name; } }
public bool IsParent { get { return true; } }
public int OrdinalNumber { get { return _ordinalNumber; } }
}
public class MintAction : IMintEntity
{
private readonly string _name;
private readonly int _ordinalNumber;
public MintAction(string actionName, int ordinalNumber)
{
_name = actionName;
if (ordinalNumber <= 0)
throw new ArgumentException("Ordinal numbers must begins from 1");
_ordinalNumber = ordinalNumber;
}
public string Name { get { return _name; } }
public bool IsParent { get { return false; } }
public int OrdinalNumber { get { return _ordinalNumber; } }
}
顺便说一句,我还在下面创建了一个接口,它实现了所有实体。这样的界面可以在将来为您提供帮助。仍然不确定,我是否也应该添加到那里Childrens
的财产List<IMintEntity>
类型,或者类似的东西?
public interface IMintEntity
{
string Name { get; }
bool IsParent { get; }
int OrdinalNumber { get; }
}
-
DLL——构建数据模型:DLL有一个方法可以打开带有单元测试和枚举数据的DLL。在枚举过程中,它会构建如下所示的数据模型。给出了真实的方法示例,使用了反射核心+ Mono.Reflection.dll,不要与复杂性相混淆。您所需要的一切 - 看看该方法如何填充
_fixtures
包含实体的列表。
private void ParseDllToEntityModel()
{
_fixutres = new List<MintFixutre>();
// enumerating Fixtures
int f = 1;
foreach (Type fixture in AssemblyTests.GetTypes().Where(t => t.GetCustomAttributes(typeof(TestFixtureAttribute), false).Length > 0))
{
var tempFixture = new MintFixutre(fixture.Name, f);
// enumerating Test Methods
int t = 1;
foreach (var testMethod in fixture.GetMethods().Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Length > 0))
{
// filtering Actions
var instructions = testMethod.GetInstructions().Where(
i => i.OpCode.Name.Equals("newobj") && ((ConstructorInfo)i.Operand).DeclaringType.IsSubclassOf(typeof(BaseAction))).ToList();
var tempTest = new MintTest(testMethod.Name, t);
// enumerating Actions
for ( int a = 1; a <= instructions.Count; a++ )
{
Instruction action = instructions[a-1];
string actionName = (action.Operand as ConstructorInfo).DeclaringType.Name;
var tempAction = new MintAction(actionName, a);
tempTest.Actions.Add(tempAction);
}
tempFixture.Tests.Add(tempTest);
t++;
}
_fixutres.Add(tempFixture);
f++;
}
}
-
DLL: 公共财产
Fixtures
of the List<MintFixutre>
创建类型以返回刚刚创建的数据模型(夹具列表,其中包含测试列表,其中包含操作列表)。这将是我们的绑定源TreeView
.
public List<MintFixutre> Fixtures
{
get { return _fixtures; }
}
-
MainWindow的ViewModel(里面有TreeView):包含 DLL 中的对象/类,可以解析单元测试 DLL。还暴露了
Fixtures
DLL 中的公共属性List<MintFixutre>
类型。我们将从 MainWindow 的 XAML 绑定到它。类似的东西(简化):
var _exporter = MySuperDllReaderExporterClass ();
// public property of ViewModel for TreeView, which returns property from #4
public List<MintFixture> Fixtures { get { return _exporter.Fixtures; }}
// Initializing exporter class, ParseDllToEntityModel() is called inside getter
// (from step #3). Cool, we have entity model for binding.
_exporter.PathToDll = @"open file dialog can help";
// Notifying all those how are bound to the Fixtures property, there are work for them, TreeView, are u listening?
// will be faced later in this article, anticipating events
OnPropertyChanged("Fixtures");
-
MainWindow 的 XAML - 设置数据模板:在包含 TreeView 的 Grid 内,我们创建<Grid.Resources>
部分,其中包含我们的一组模板TreeViewItem
s. HierarchicalDataTemplate
(固定装置和测试)用于那些有儿童物品的人,并且DataTemplate
用于“叶”项(操作)。对于每个模板,我们指定其内容(文本、TreeViewItem 图像等)、ItemsSource(如果该项目有子项,例如对于 Fixtures,它是{Binding Path=Tests}
)和 ItemTemplate (同样,只有在该项目有子项的情况下,这里我们设置模板之间的链接 - FixtureTemplate 使用 TestTemplate 作为其子项,TestTemplate 使用 ActionTemplate 作为其子项,Action 模板不使用任何内容,它是叶子!)。重要提示:不要忘记,为了将“一个”模板“链接”到“另一个”模板,“另一个”模板必须在 XAML 中定义在“一个”之上! (只是列举我自己的错误:))
-
XAML - TreeView 链接:我们设置 TreeView 的方法是:与 ViewModel 中的数据模型(还记得公共属性吗?)以及刚刚准备好的模板链接,这些模板表示内容、外观、数据源和树项的嵌套!还有一个重要的注意事项。不要将 ViewModel 定义为 XAML 中的“静态”资源,例如<Window.Resources><MyViewModel x:Key="DontUseMeForThat" /></Window.Resources>
。如果您这样做,那么您将无法在属性更改时通知它。为什么?静态资源就是静态资源,它初始化了,之后就剩下了不可变的。我在这里可能是错的,但这是我的错误之一。所以对于 TreeView 使用ItemsSource="{Binding Fixtures}"
代替ItemsSource="{StaticResource myStaticViewModel}"
ViewModel - ViewModelBase - 属性已更改: 几乎全部。停止!当用户打开应用程序时,最初 TreeView 当然是空的,因为用户还没有打开任何 DLL!我们必须等到用户打开一个DLL,然后才执行绑定。它是通过以下方式完成的OnPropertyChanged
事件。为了让生活更轻松,我所有的 ViewModel 都是从 ViewModelBase 继承的,它可以将此功能公开给我的所有 ViewModel。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, args);
}
}
-
XAML - OnPropertyChanged 和命令。用户单击按钮打开包含单元测试数据的 DLL。当我们使用MVVM
,然后通过命令处理单击。结束时OpenDllExecuted
处理程序OnPropertyChanged("Fixtures")
被执行,通知树,它所绑定到的属性已经改变,现在是时候刷新它自己了。RelayCommand
辅助类可以取自there)。顺便说一句,据我所知,存在一些辅助库和工具包,XAML 中会发生类似的情况:
和 ViewModel - 指挥
private ICommand _openDllCommand;
//...
public ICommand OpenDllCommand
{
get { return _openDllCommand ?? (_openDllCommand = new RelayCommand(OpenDllExecuted, OpenDllCanExecute)); }
}
//...
// decides, when the <OpenDll> button is enabled or not
private bool OpenDllCanExecute(object obj)
{
return true; // always true for Open DLL button
}
//...
// in fact, handler
private void OpenDllExecuted(object obj)
{
var openDlg = new OpenFileDialog { ... };
_pathToDll = openDlg.FileName;
_exporter.PathToDll = _pathToDll;
// Notifying TreeView via binding that the property <Fixtures> has been changed,
// thereby forcing the tree to refresh itself
OnPropertyChanged("Fixtures");
}
-
Final UI(但对我来说还不是最终的,还有很多事情要做!)。扩展的 WPF 工具包在某处使用:http://wpftoolkit.codeplex.com/