c# - 已启用 UI 自动化按钮样式

标签 c# testing ui-automation

我正在评估用于 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);

但在我的例子中,按钮始终处于启用状态,因此我需要检查应用于按钮的样式。

非常感谢。

最好的问候

最佳答案

正如此链接中的评论: http://social.msdn.microsoft.com/Forums/en/windowsaccessibilityandautomation/thread/129d2ea6-91ae-4f65-b07e-c36684ae742b

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/

相关文章:

c# - 如何在 .NET 4 中使用 Console.CancelKeyPress? (在 .NET 3.5 及以下版本中工作正常)

c# - 使用自定义对象创建嵌套的 ObservableCollections

asp.net-mvc-3 - 单元测试一个文件上传,怎么样?

c++ - 如何使用 gmock 框架设置通过引用发送的参数?

python - Nose 报告累积覆盖率

java - 如何处理过时的元素引用: element is not attached to the page document

wpf - 如何在对项目进行分组的 WPF ItemsControl 上使用 UI 自动化?

c# - 在 C# asp.net 中使用 sqlbulkcopy 将 xml 数据写入 sql server

c# - "Compile with/main to specify the type that contains the entry point."

javascript - Xcode 中的 UI 自动化