我正在评估用于 UI 测试的 UI 自动化,因为我有一个定义了以下按钮的 WPF 应用程序:
<Button Style="{DynamicResource ButtonStyle}" x:Name="MyBtn"/>
当我需要在视觉上禁用按钮时,我只是更改样式,以便用户知道按钮已禁用(颜色已更改),但按钮仍然在内部启用,因此我仍然可以启动 OnClick 事件以显示当用户点击“已禁用”按钮时的消息。
现在的问题是我不知道如何从 UI Automation 检查其当前应用的样式,即按钮是禁用还是启用。你知道我该怎么做吗?
在正常情况下我应该这样做:
Automation.Condition cEBtn = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyBtn");
AutomationElement mybtnElement = appRegraceElement.FindFirst(TreeScope.Children, cEBtn);
bool disMyBtn = (bool)mybtnElement .GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);
但在我的例子中,按钮始终处于启用状态,因此我需要检查应用于按钮的样式。
非常感谢。
最好的问候
最佳答案
WPF 属性(还)不能公开为自动化属性。尽管如此,Michael 还是提出了一个解决方法。我会把它留在这里以防万一对某人有用。
<Style TargetType="Button">
<Setter Property="AutomationProperties.ItemStatus"
Value="{Binding RelativeSource={RelativeSource Self}, Path=Style}" />
</Style>
如您所见,我们在这里所做的是使用 Automation 属性 ItemStatus 公开(针对所有按钮)WPF 属性 Style。然后可以像这样从 UI Automation 客户端获取此 Style:
Automation.Condition cEBtn = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyBtn");
AutomationElement mybtnElement = appRegraceElement.FindFirst(TreeScope.Children, cEBtn);
string a = (string)mybtnElement.GetCurrentPropertyValue(AutomationElement.ItemStatusProperty);
作为一种解决方法,它对我来说没问题,但它有两个问题,它需要我更新应用程序代码,在测试期间应该不是必需的,并且它一次只能公开一个属性。
最好的问候, 维克多
关于c# - 已启用 UI 自动化按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2684112/