开发者

UI Automation Button Style Enabled

I'm evaluating UI Automation for UI testing for that I have a WPF application with the following button defined:

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

when I need to visually disable the button I just change the style so the user is aware that the button is disabled (the colour changed) but still the button is internally enabled so I can still launch the OnClick event in order to show a message when the user clicks on a "disabled" button.

Now the problem is that I don't know how to check from UI Automation the Style that its currently applied i.e. if the button is disabled or enabled. Do you know how can I do that?

In a normal situation I should do something like that:

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

Automat开发者_运维知识库ionElement mybtnElement = appRegraceElement.FindFirst(TreeScope.Children, cEBtn);

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

but in my case the button is always enabled therefore I need to check the Style applied to the button.

Thank you very much.

Best regards


as commented in this link: http://social.msdn.microsoft.com/Forums/en/windowsaccessibilityandautomation/thread/129d2ea6-91ae-4f65-b07e-c36684ae742b

WPF properties cannot be (yet) exposed as Automation properties. Nevertheless, Michael proposes a workaround. I'll leave it here just in case is useful for someone.

<Style TargetType="Button">
    <Setter Property="AutomationProperties.ItemStatus"
        Value="{Binding RelativeSource={RelativeSource Self}, Path=Style}" />
</Style>

as you can see what we are doing here is to expose (for all the buttons) the WPF property Style using the Automation property ItemStatus. Then this Style can be obtained from UI Automation client like that:

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

as a workaround its ok for me, but it has two problems, it requires me to update the application code, should not be necessary during testing, AND it can only expose one property at a time.

Best regards, Víctor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜