我刚刚开始使用AutomationElement,因为我们希望对自定义控件进行集成测试,我认为我应该使用AutomationElement。
我已经成功地创建了一个带有自定义控件的窗口,并且可以成功地获得窗口和控件的AutomationElements。
// Retrieve the View
System.Windows.Automation.Condition viewCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyTestView");
AutomationElement view = AutomationElement.RootElement.FindFirst(TreeScope.Children, viewCondition);
Assert.IsNotNull(view);
// Retrieve the CustomControl
System.Windows.Automation.Condition comboboxCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyCustomControl");
AutomationElement combobox = view.FindFirst(TreeScope.Children, comboboxCondition);
Assert.IsNotNull(comboboxCondition);现在,我想要做的是使用,例如ValuePattern。这就是我感到困惑的地方。
为了寻找信息,我在referencesource.microsoft.com上搜索了WPF的源代码。我遇到了ComboboxAutomationPeer,它实现了IValueProvider,所以现在我很困惑。
我是否也应该实现实现IValueProvider的IValueProvider,然后AutomationElement是否会与ValuePattern一起工作?还是应该让MyCustomControl实现IValueProvider?
发布于 2015-06-26 18:12:56
您不需要实现任何东西就可以使用模式。UI自动化为您(充当目标应用程序的代理)做到了这一点。这在这里的正式文档中得到了很好的解释:获得支持的UI自动化控制模式
下面是一个示例摘录:
SelectionItemPattern pattern;
try
{
pattern = yourAutomationElement.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message); // Most likely "Pattern not supported."
return;
}
pattern.Select();https://stackoverflow.com/questions/31077082
复制相似问题