2

我正在评估 UI 自动化以进行 UI 测试,因为我有一个定义了以下按钮的 WPF 应用程序:

<Button Style="{DynamicResource ButtonStyle}" x:Name="MyBtn"/>

当我需要在视觉上禁用该按钮时,我只需更改样式,以便用户知道该按钮已禁用(颜色已更改),但该按钮仍然在内部启用,因此我仍然可以启动 OnClick 事件以便在何时显示消息用户单击“禁用”按钮。

现在的问题是我不知道如何从 UI 自动化中检查其当前应用的样式,即按钮是否被禁用或启用。你知道我该怎么做吗?

在正常情况下,我应该这样做:

Automation.Condition cEBtn = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyBtn");

AutomationElement mybtnElement = appRegraceElement.FindFirst(TreeScope.Children, cEBtn);

bool disMyBtn = (bool)mybtnElement .GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);

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

非常感谢你。

最好的祝福

4

1 回答 1

1

如此链接中所述: 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>

如您所见,我们在这里所做的是使用自动化属性 ItemStatus 公开(对于所有按钮)WPF 属性 Style。然后可以像这样从 UI 自动化客户端获取此样式:

Automation.Condition cEBtn = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyBtn");
AutomationElement mybtnElement = appRegraceElement.FindFirst(TreeScope.Children, cEBtn);
string a = (string)mybtnElement.GetCurrentPropertyValue(AutomationElement.ItemStatusProperty);

作为一种解决方法,它对我来说没问题,但它有两个问题,它需要我更新应用程序代码,在测试期间应该不需要,并且一次只能公开一个属性。

最好的问候,维克多

于 2010-04-23T14:22:20.393 回答